Bash: Parse CSV with quotes, commas and newlines

2020-04-02 07:43发布

Say I have the following csv file:

 id,message,time
 123,"Sorry, This message
 has commas and newlines",2016-03-28T20:26:39
 456,"It makes the problem non-trivial",2016-03-28T20:26:41

I want to write a bash command that will return only the time column. i.e.

time
2016-03-28T20:26:39
2016-03-28T20:26:41

What is the most straight forward way to do this? You can assume the availability of standard unix utils such as awk, gawk, cut, grep, etc.

Note the presence of "" which escape , and newline characters which make trivial attempts with

cut -d , -f 3 file.csv

futile.

标签: bash csv awk cut gawk
7条回答
何必那么认真
2楼-- · 2020-04-02 08:23
sed -e 's/,/\n/g' file.csv | egrep ^201[0-9]-
查看更多
登录 后发表回答