1

Made a file.csv. There are 3 lines.

"ip","count","filename"
"1.1.1.1","57","somefilename1"
"2.2.2.2","30","2somefilename-2"

Tryed to awk for $3 so it should just list the 3rd column.

I tried a couple of methods:

awk '{print $3}' filename.csv  

Output is blank, I was expecting just the 3rd column

Tried:

awk '{print $1}' filename.csv

Shows the full document instead of just the 1st column, nothing got filtered.

I searched stackoverflow and tried a few other methods. But for some reason I either get the full row or nothing.

What am I missing sounds simple enough

chowpay
  • 1,515
  • 6
  • 22
  • 44
  • 1
    `awk -v FS="," '{print $3}' filename.csv` You need to tell `awk` what to use as field seprator(`FS`) . By default its white space. – P.... Jan 30 '18 at 06:19
  • 1
    The default field delimiter in Awk is a sequence of whitespace. You are missing `awk -F,` to use comma instead. Voting to close as trivial typo. – tripleee Jan 30 '18 at 06:19
  • Your input file has unicode quotes(`”`) and not ascii quotes(`"`), did you copy paste this file from a DOS editor? (windows - notepad/notepad++) – Inian Jan 30 '18 at 06:19
  • 1
    Possible duplicate of [How to get the first column of every line from a CSV file?](https://stackoverflow.com/questions/11668621/how-to-get-the-first-column-of-every-line-from-a-csv-file) – Sundeep Jan 30 '18 at 06:27
  • @PS Thanks that was it. Does the S in FS allow you to use a string or something longer than 1 non-spaced comma as a separator? – chowpay Jan 30 '18 at 06:45
  • 1
    @chowpay you can use string as `FS` but be careful. Also, best way to find out is to try. – P.... Jan 30 '18 at 07:01

4 Answers4

3

You should use -F as for "field-separator", you can find this at the man page of awk, like this:

awk -F, '{print $3}' Input_file
”filename”
”somefilename1”
”2somefilename-2”
Colin Moreno Burgess
  • 1,432
  • 1
  • 12
  • 17
1

Use , as field separator:

awk -F ',' '{print $3}' filename.csv 
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

Following awk solutions may help you in same. If you want to get output with " then following may help:

awk -F, '{print $3}'  Input_file

In case you need values of 3rd column without " then following may help you:

awk -F',|"' '{print $(NF-1)}'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

By default awk uses space as field separator, as described in the other posts you need to configure , as field separator otherwise the whole line will be treated as one field. To do this you can choose between the following 2 syntax:

awk -F',' '{print $3}' input.csv #to avoid bad surprises with you shell trying to interpret your field separator put it between simple quotes.  

or you can also defined in the BEGIN clause, or at any point during the execution using the following syntax:

awk 'BEGIN{FS=","}{print $3}' input.csv
Allan
  • 12,117
  • 3
  • 27
  • 51