如何在csv中将列的所有数据保存在一行中

时间:2015-07-18 14:55:15

标签: linux perl csv awk

我有一个csv文件,它有2列id,notetext。在第二列(notetext)中,数据有多行。我想要的是将所有数据放在一行。

以下是样本:

**catalog_SID, Note_Text**
584,"Sample in a Glove Bag only.  Refer to SOP
489-701 for sampling
great
procedure."
647,"Do not use
hotplate"
680,Sample in a Glove Bag

我需要的是notetext中的所有数据,如下所示:

**catalog_SID, Note_Text**
584,"Sample in a Glove Bag only.Refer to SOP 489-701 for sampling."
647,"Do not use hotplate"
680,Sample in a Glove Bag

知道怎么做到这一点吗?

2 个答案:

答案 0 :(得分:1)

只需读取整个文件并用空格替换所有换行符,如果它们后面没有数字和逗号

perl -0777 -pe's/\n(?!\d+,)/ /g' myfile

输出

catalog_SID, Note_Text
584,"Sample in a Glove Bag only.  Refer to SOP 489-701 for sampling great procedure."
647,"Do not use hotplate"
680,Sample in a Glove Bag

答案 1 :(得分:0)

只要记住到目前为止看到多少引号,如果该数字甚至在当前行的末尾添加换行符,否则为空格:

$ awk '{ORS=( (c+=gsub(/"/,"&"))%2 ? FS : RS )} 1' file
**catalog_SID, Note_Text**
584,"Sample in a Glove Bag only.  Refer to SOP 489-701 for sampling great procedure."
647,"Do not use hotplate"
680,Sample in a Glove Bag