使用 bash 将 .CSV 文件转换为 .txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28319192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:19:23  来源:igfitidea点击:

Convert .CSV file to .txt file using bash

bashunixcsvtext

提问by Nelson Lang

I have a CSV with lost of lines delimited by comma. I have to convert it to text.

我有一个 CSV,其中丢失了以逗号分隔的行。我必须将其转换为文本。

66012523,39,Feb 02 2015 05:19AM,
66012523,39,Feb 02 2015 09:53AM,
66012523,39,Feb 02 2015 01:38PM,

I used command cp source.csv destination.csvand also cat source.csv > destination.txtbut it does output in the same format witch each line coming in new one. It just gets appended together. It outputs like

我使用了命令cp source.csv destination.csvcat source.csv > destination.txt但它确实以相同的格式输出,每一行都是新的。它只是被附加在一起。它输出像

66012523,39,Feb 02 2015 05:19AM,66012523,39,Feb 02 2015 09:53AM,66012523,39,Feb 02 2015 01:38PM

How do I make them to output each line in newline. Please help.

如何让它们以换行符输出每一行。请帮忙。

回答by Mark Smith

I hypothesise that the first block in your question is what you WANT, and what you actually HAVE is

我假设您问题中的第一个块是您想要的,而您实际上拥有的是

66012523,39,Feb 02 2015 05:19AM,66012523,39,Feb 02 2015 09:53AM,66012523,39,Feb 02 2015 01:38PM

So what you want to do, I hypothesise, is split this up into separate lines.

所以你想要做的,我假设,是把它分成单独的行。

Am I right?

我对吗?

This is a bit rough-and-ready but it works. Relies on each group ending "M," and there being no other "M,"s in the text - which there aren't but I wouldn't call it robust.

这有点粗略,但它有效。依赖于以“M”结尾的每个组,并且文本中没有其他“M”——没有但我不会称之为健壮。

sed s/M,/'\n'/g source.csv