简体   繁体   English

如何在bash中将以竖线分隔的文件转换为CSV格式?

[英]How to convert pipe-delimited file to CSV format in bash?

I have a gigantic 300MB text file that is a pipe delimited CSV. 我有一个300MB的巨大文本文件,它是用竖线分隔的CSV文件。

Some Writing, Is|Another Field|Anotherfie,ld.
Some Writing, Is|Another Field|Anotherfie,ld.

Is there a way in bash shell to convert it to: bash shell中是否可以将其转换为:

"Some Writing, ","Another Field","Anotherfie,ld."

With sed: 与sed:

sed 's/^/"/;s/|/","/g;s/$/"/' file

Output: 输出:

"Some Writing, Is","Another Field","Anotherfie,ld."
"Some Writing, Is","Another Field","Anotherfie,ld."

If you want to edit your file "in place" add sed's option -i . 如果要“就地”编辑文件,请添加sed的选项-i

Using awk you can do this: 使用awk,您可以执行以下操作:

awk -F '|' -v OFS=, '{for(i=1; i<=NF; i++) $i="\"" $i "\""} 1' file.csv
"Some Writing, Is","Another Field","Anotherfie,ld."
"Some Writing, Is","Another Field","Anotherfie,ld."

You can use sed and it's substitution commands: 您可以使用sed及其替代命令:

s/^/"/ will replace (insert) a " sign at the beginning of each line. s/^/"/将替换(插入)每行开头的"符号。
s/|/","/g will replace each | s/|/","/g将替换每个| character with "," triplet. 带有","三元组的字符。 Note the g letter at the end of command, which instructs it to replace every | 注意命令末尾的g字母,该字母指示它替换每个 | occurrence, not only the first one. 发生,不仅是第一个。
And finally: 最后:
s/$/"/ will replace (append) a " sign at the end of each line. s/$/"/将替换(附加)每行末尾的"符号。

So final command will be: 因此,最终命令将是:

`cat filename.in | sed 's/^/"/;s/|/","/g;s/$/"/` > filename.out`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM