0

I have a csv file containing

a,b,c,d,e,f,"gg,hh,ii",j,k

I have been trying to use something like

awk -F ',' '{print $7}'

iam getting

"gg

but i need the result as

gg,hh,ii

can i get it done with a simple one line awk or sed command without any parsers

please help

  • 1
    For future reference: Aks unix questions on https://unix.stackexchange.com. For example, https://unix.stackexchange.com/questions/114754/extract-only-the-substring-after-double-quotes-grep answers your question. `echo 'a,b,c,d,e,f,"gg,hh,ii",j,k' | awk -F\" '{print $2}'` – Oliver Jan Krylow Dec 11 '17 at 11:22
  • `sed 's/[^"]*"\([^"]*\).*/\1/'` i read this somewhere....can I modify it to delete a particular column alone? –  Dec 11 '17 at 11:33
  • @Ajit can you clarify your question? title says delete a column while expected output shows extracting a particular column – Sundeep Dec 11 '17 at 11:41
  • `sed 's/[^"]*"//; s/".*//'` would work for given sample, but that may not work for actual use case.. you might want to add more examples – Sundeep Dec 11 '17 at 11:51

2 Answers2

1

Perl to the rescue!

perl -MText::CSV_XS=csv -we 'csv(in => shift, fragment => "col=7")' -- file.csv

See Text::CSV_XS for details. The fragment option tells thecsv function to only output the 7th column.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • Can't locate Text/CSV_XS.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .). BEGIN failed--compilation aborted. –  Dec 11 '17 at 11:14
  • @Ajit see https://stackoverflow.com/questions/65865/whats-the-easiest-way-to-install-a-missing-perl-module – Sundeep Dec 11 '17 at 11:40
0

You can use this:

awk -vFPAT='[^,]*|"[^"]*"' '{print $7}' file.csv

Use FPAT to define the content of the field instead of defining the field separator FS (with -F option).

For the awk man page:

FPAT: A regular expression describing the contents of the fields in a record. When set, gawk parses the input into fields, where the fields match the regular expression, instead of using the value of the FS variable as the field separator.

oliv
  • 12,690
  • 25
  • 45