Reputation: 2083
I have a CSV data file say with 5 columns separated by comma.
c1, c2, col3, c4, c5
stack, over, upon, true, yes
ab, zy, pq,rs,tu,vw,ef, four, ivef
Viewing the csv file in Excel will clearly show those 5 columns, and the 3rd field having the following values: pq,rs,tu,vw,ef.
However, how do I get awk to print out col3 ($3) with "pq,rs,tu,vw,ef" as output. Right now, it sees it as pq. And the rest have shifted out of place.
Updated csv sample:
Movie ID,Remit ID,Property ID,Movie Uploader,Channel ID,Channel Display Name,Video Title,Views Count,Status,Claim Origin,Claim Type,Is Affiliate Uploaded,Is Premium,Reference Movie ID,Policy,Applied Policy,Claim Date,Movie Upload Date,Custom ID,EWRC,Title,Authors,Notes,Asset Labels
G4pelo5M9XI,ka-9foAPFkg,N103145385208693,originalkaraoke,UCnF6KQeanPgBRyEMeFmrNnA,Karaoke,Motel Fornia - Karaoke,6702511,Active,Descriptive Search,AudioVisual,No,No,,,Block the following countries: US; Track in all countries except: US,2017/01/25,2011/12/30,fW1aUnBbwL8,,MOTEL FORNIA - BLOCK,,,
uZ94drkfB5c,WIMPvt22JY8,B103945385208693,,UCBa3saYRQTO8WzsKacgaJNQ,Best Songs Backing Tracks,"Motel Fornia - Bass Backing Track with scale, chords and lyrics",1913,Active,Descriptive Search,AudioVisual,No,No,,,Track in all countries except: US; Block the following countries: US,2017/01/25,2016/01/19,fW1aUqBzwL2,,MOTEL FORNIA - BLOCK,,,
2p1te0kAE2A,HMR7M2SjJJw,N103945385208693,,UCLAvPQhYyx8yUNMG0AkPYuw,Jordy Nalgas,HOSTEL NARNIA,751,Active,Descriptive Search,AudioVisual,No,No,,,Block the following countries: US; Track in all countries except: US,2017/01/25,2016/09/11,fW1dUnBhwL8,,HOSTEL NARNIA - BLOCK,,,
and we need to extract value of Views Count
column.
Upvotes: 0
Views: 64
Reputation: 785631
In gnu awk
you can use FPAT
to tell awk what is a valid column expression.
You can use:
awk -v col='Views Count' -v FPAT='"[^"]*"|[^,]*' '
NR==1{for (h=1; h<=NF; h++) if ($h == col) break; next} {print $h}' file.csv
6702511
1913
751
If you want column heading also then remove next
from awk script above.
Upvotes: 1