2

I have a file with the following content:

"aaa" "aaa bbb" "c cc  c" "ddd"

I'm looking for command that will show me specified column.

I try to use:

awk '{print $1}'

But it doesn't work properly if there is a space char in doublequetoes like "c cc c".

Thanks in advance for help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3461823
  • 1,303
  • 2
  • 11
  • 17
  • so how do you think awk recognizes columns? – Karoly Horvath Sep 18 '15 at 07:51
  • Hi Karoly, I know how it works(it uses spaces) and I know that it is possible to define own field seperator. I don't know only how to define such separator. – user3461823 Sep 18 '15 at 07:58
  • https://www.google.com/search?q=awk+define+own+field+seperator (sic!) – Karoly Horvath Sep 18 '15 at 08:16
  • Karoly, don't be so mean. Pasting link to google you could answer on all question on stackoverflow, but it doesn't help anyone. – user3461823 Sep 18 '15 at 10:06
  • That's why we expect quality questions which cannot be answered by simply googling it. Creating duplicate questions doesn't help anyone.... and as a sidenote, searching is an immensely important and useful skill, something that all beginners have to learn. There's nothing mean about showing someone how to find something. – Karoly Horvath Sep 18 '15 at 10:18
  • @tripleee it isn't duplicate from [Parse a csv using awk and ignoring commas inside a field](http://stackoverflow.com/questions/4205431/parse-a-csv-using-awk-and-ignoring-commas-inside-a-field), they are using [AWK CSV Parser](http://lorance.freeshell.org/csv/) – Jose Ricardo Bustos M. Sep 18 '15 at 11:57
  • @JoseRicardoBustosM. The fact that your field separator isn't a comma means your input isn't strictly CSV, but the problem and the solutions are fundamentally the same regardless. This is a very common question; if you don't like that particular duplicate, please do suggest a different one. – tripleee Sep 18 '15 at 13:52

3 Answers3

4

You can use FPAT variable in gnu-awk to break the columns based on a regex:

s='"aaa" "aaa bbb" "c cc  c" "ddd"'

awk -v FPAT='"[^"]+"' '{for (i=1; i<=NF; i++) printf "Field %d:: <%s>\n", i, $i}' <<< "$s"
Field 1:: <"aaa">
Field 2:: <"aaa bbb">
Field 3:: <"c cc  c">
Field 4:: <"ddd">

-v FPAT='"[^"]+"' will set each field value between 2 double quotes.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

A solution using traditional awk

echo '"aaa" "aaa bbb" "c cc  c" "ddd"' | 
awk -c -F "^\"|\"[ \t]+\"|\"$" '{
    for(i=2; i<=NF-1; i++) 
        printf "\"%s\"\n", $i 
}'

you get:

"aaa"
"aaa bbb"
"c cc  c"
"ddd"
Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62
0

Perl to the rescue! Use the Text::CSV module:

perl -MText::CSV -lwe '
    my $csv = Text::CSV->new({sep_char => " "}) or die Text::CSV->error_diag;
    open my $FH, "<", "input.txt" or die $!;
    while (my $row = $csv->getline($FH)) {
        print $row->[3];
    }'
choroba
  • 231,213
  • 25
  • 204
  • 289