Search code examples
awksedgrepechocat

How to use awk to filter a .csv file


I was wondering how can I get the names of the fruits in from this .csv file by using awk or some other cli tool.

I used a macro in vim to edit the file, but I would think that there is an easy one liner that would do the same.

fruits.csv:

"1000","Apple","4","133"
"1028","Lemon","3","120"
"1029","Lime","3","165"
"1030","Lychee","6","120"
"1031","Mango","6","131"
"1032","Mangostine","1","181"
"1033","Melon","4","159"
"1034","Cantaloupe","4","138"
"1035","Honeydew melon","4","155"
"1036","Watermelon","5","176"
"1037","Rock melon","2","180"
"1038","Nectarine","1","128"
"1039","Orange","6","142"
"1040","Peach","6","179"
"1041","Pear","3","102"
"1042","Williams pear or Bartlett pear","1","164"
"1043","Pitaya","2","170"
"1044","Physalis","5","166"
"1045","Plum/prune (dried plum)","4","103"
"1046","Pineapple","3","120"
"1047","Pomegranate","5","112"
"1048","Raisin","4","111"
"1049","Raspberry","5","156"
"1050","Western raspberry (blackcap)","6","173"

The final result that I would want would look like this:

Apple
Lemon
Lime
Lychee
Mango
Mangostine
Melon
Cantaloupe
Honeydew melon
Watermelon
Rock melon
Nectarine
Orange
Peach
Pear
Williams pear or Bartlett pear
Pitaya
Physalis
Plum/prune (dried plum)
Pineapple
Pomegranate
Raisin
Raspberry
Western raspberry (blackcap)

I realize that this is a duplicate:

What's the most robust way to efficiently parse CSV using awk?

How to parse a CSV in a Bash script?


Solution

  • I suggest:

    awk -F '","' '{print $2}' file
    

    Use "," as field separator and output second column.