假设我有以下 csv 文件:
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
我想编写一个只返回时间列的 bash 命令。即
time
2016-03-28T20:26:39
2016-03-28T20:26:41
最直接的方法是什么?您可以假定标准 unix 实用程序的可用性,例如 awk、gawk、cut、grep 等。
请注意转义的 ""和用
进行微不足道尝试的换行符的存在cut -d , -f 3 file.csv
徒劳的。
最佳答案
作为chepner said ,我们鼓励您使用能够解析 csv 的编程语言。
这里有一个python的例子:
import csv
with open('a.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, quotechar='"')
for row in reader:
print(row[-1]) # row[-1] gives the last column
关于Bash:用引号、逗号和换行符解析 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36287982/