本文介绍了Bash:用引号、逗号和换行符解析 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下 csv 文件:

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

我想编写一个只返回时间列的 bash 命令.即

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

最直接的方法是什么?您可以假设标准 unix 实用程序(例如 awk、gawk、cut、grep 等)的可用性.

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

没用.

推荐答案

As chepner 说,鼓励您使用能够解析 csv 的编程语言.

As chepner said, you are encouraged to use a programming language which is able to parse csv.

这里有一个python的例子:

Here comes an example in 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 03:12