Reputation: 23
I have two csv files:
The first one, let's call it file1.csv, has the information that I need and it's something like:
Cell,Bitrate,Loss Ratio,Retransmitted Ratio
MI456,400,0.6,2.3,....
MI457,400,0.6,2.3,...
MI458,400,0.6,2.3,...
.
.
.
The second one, file2.csv,has the subset of cells I need to extract from file1, and it is something like this:
Cell
MI400
CA500
VE600
And I want my output to be:
Cell,Bitrate,Loss Ratio,Retransmitted Ratio
MI400,400,0.6,2.3,....
CA500,400,0.6,2.3,...
VE600,400,0.6,2.3,...
I've trying to use:
cat file1.csv | grep -f file2.csv
or grep -f
but without success, could you please help me?
I'm using Linux 2.6.31.2 x86_64 GNU/Linux
Upvotes: 2
Views: 2573
Reputation: 246942
{
sed 1q file1.csv
join -t, <(sed 1d file1.csv | sort) <(sed 1d file2.csv | sort)
} > newfile.csv
join
requires the input files to be sorted. I'm using sed
to delete the header line inside the process substitutions.
Upvotes: 0
Reputation: 201
maybe you are using the -f
option in the wrong way
Here is an example:
file1:
Cell,Bitrate,Loss Ratio,Retransmitted Ratio
MI456,400,0.6,2.3,....
MI457,400,0.6,2.3,...
MI458,400,0.6,2.3,...
MI465,400,0.6,2.3,...
MI477,400,0.6,2.3,...
file2:
MI456
MI457
MI465
grep:
grep -f file2 file1
MI456,400,0.6,2.3,....
MI457,400,0.6,2.3,...
MI465,400,0.6,2.3,...
Upvotes: 0
Reputation: 5412
for FOO in `cat file2.csv`; do
grep "^$FOO" file1.csv
done
This will be very slow if file2.csv has lots of rows.
Upvotes: 0
Reputation: 121397
This will replace the first column in file1.csv with the contents of the file2.csv:
cut -d',' -f2- file1.csv | paste -d',' file2.csv -
Upvotes: 1