Can I use grep to extract a single column of a CSV

2020-03-30 03:57发布

问题:

I'm trying to solve o problem I have to do as soon as possible. I have a csv file, fields separated by ;. I'm asked to make a shell command using grep to list only the third column, using regex. I can't use cut. It is an exercise.

My file is like this:

1;Evan;Bell;39;Obigod Manor;Ekjipih;TN;25008
2;Wayne;Watkins;22;Lanme Place;Cotoiwi;NC;86578
3;Danny;Vega;25;Fofci Center;Momahbih;MS;21027
4;Larry;Robinson;23;Bammek Boulevard;Gaizatoh;NE;27517
5;Myrtie;Black;20;Savon Square;Gokubpat;PA;92219
6;Nellie;Greene;23;Utebu Plaza;Rotvezri;VA;17526
7;Clyde;Reynolds;19;Lupow Ridge;Kedkuha;WI;29749
8;Calvin;Reyes;47;Paad Loop;Beejdij;KS;29247
9;Douglas;Graves;43;Gouk Square;Sekolim;NY;13226
10;Josephine;Estrada;48;Ocgig Pike;Beheho;WI;87305
11;Eugene;Matthews;26;Daew Drive;Riftemij;ME;93302
12;Stanley;Tucker;54;Cure View;Woocabu;OH;45475
13;Lina;Holloway;41;Sajric River;Furutwe;ME;62184
14;Hettie;Carlson;57;Zuheho Pike;Gokrobo;PA;89098
15;Maud;Phelps;57;Lafni Drive;Gokemu;MD;87066
16;Della;Roberson;53;Zafe Glen;Celoshuv;WV;56749
17;Cory;Roberson;56;Riltav Manor;Uwsupep;LA;07983
18;Stella;Hayes;30;Omki Square;Figjitu;GA;35813
19;Robert;Griffin;22;Kiroc Road;Wiregu;OH;39594
20;Clyde;Reynolds;19;Lupow Ridge;Kedkuha;WI;29749
21;Calvin;Reyes;47;Paad Loop;Beejdij;KS;29247
22;Douglas;Graves;43;Gouk Square;Sekolim;NY;13226
23;Josephine;Estrada;48;Ocgig Pike;Beheho;WI;87305
24;Eugene;Matthews;26;Daew Drive;Riftemij;ME;93302

I think I should use something like: cat < test.csv | grep 'regex'.

Thanks.

回答1:

Right Tools For The Job: Using awk or cut

Assuming you want to match the third column against a specific field:

awk -F';' '$3 ~ /Foo/ { print $0 }' file.txt

...will print any line where the third field contains Foo. (Changing print $0 to print $3 would print only that third field).

If you just want to print the third column regardless, use cut: cut -d';' -f3 <file.txt

Wrong Tool For The Job: Using GNU grep

On a system where grep has the -o option, you can chain two instances together -- one to trim everything after the fourth column (and remove lines with less than four columns), another to take only the last remaining column (thus, the fourth):

str='foo;bar;baz;qux;meh;whatever'
grep -Eo '^[^;]*[;][^;]*[;][^;]*[;][^;]*' <<<"$str" \
  | grep -Eo '[^;]+$'

To explain how that works:

  • ^, outside of square brackets, matches only at the beginning of a line.
  • [^;]* matches any character except ; zero-or-more times.
  • [;] matches only the character ;.

...thus, each [^;]*[;] in the regex matches a single field, whether or not that field contains text. Putting four of those in the first stage means we're matching only fields, and grep -o tells grep to only emit content it was successfully able to match.



回答2:

If you just need the 3rd field and it's always properly delimited with ';' why not use 'cut'?

cut -d';' -f3   <filename>  

UPDATED:

OP wasn't clear, maybe only want to look at the 3rd line?

head -3 <filename> | tail -1 

OR.. Maybe just getting of list of the things that appear in the 3rd field?

Not clear what the intended use of 'grep' would be??

cut -d';' -f3   <filename>  | sort -u 


回答3:

As the other answers have said, using grep is a bad/unfortunate idea.

The only way I can think of using grep is to pull out a specific row where the 3rd column == some value. E.g.,

grep '^\([^;]*;\)\{2\}Bell;' test.txt                                                           
1;Evan;Bell;39;Obigod Manor;Ekjipih;TN;25008

Or if the first column is the index (not counting it as a column):

grep '^\([^;]*;\)\{3\}39;' test.txt  
1;Evan;Bell;39;Obigod Manor;Ekjipih;TN;25008

Even using grep in this case leads to a pretty ugly solution.

Edit: Didn't see Charles Duffy's answer... that's pretty clever.



标签: shell csv grep