逗号分隔但需要排除用逗号填充的封闭字段

时间:2017-06-20 10:30:56

标签: bash awk

我有一个CSV数据文件,其中5列用逗号分隔。

SELECT * FROM table 
where 
REPLACE(name,' ','') =  'davidraj'

在Excel中查看csv文件将清楚地显示这5列,第3个字段具有以下值:pq,rs,tu,vw,ef。

但是,如何让awk以“pq,rs,tu,vw,ef”作为输出打印出col3($ 3)。现在,它将其视为pq。其余的已经不合时宜了。

更新了csv示例:

c1, c2, col3, c4, c5
stack, over, upon, true, yes
ab, zy, pq,rs,tu,vw,ef, four, ivef

我们需要提取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,,, 列的值。

1 个答案:

答案 0 :(得分:1)

gnu awk中,您可以使用FPAT告诉awk什么是有效的列表达式。

您可以使用:

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

如果您还想要列标题,请从上面的awk脚本中删除next