需要一个 shell 脚本将逗号分隔符更改为管道分隔符
我的输入看起来像 "$130.00","$2,200.00","$1,230.63"
等等 我的问题是如何将逗号分隔符更改为 |分隔符而不删除实际输入中的逗号。 只是为了澄清此输入位于具有 40 列和 9500 行的 csv 文件中。 我希望我的输出看起来像
"$130.00"|"$2,200.00"|"$1,230.63"
My input looks like "$130.00","$2,200.00","$1,230.63"
and so on
My question is how can I go about changing the comma delimiter to a | delimiter without getting rid of the comma in the actual input.
Just to clarify this input is in a csv file with 40 columns and 9500 rows.
I want my output to look like
"$130.00"|"$2,200.00"|"$1,230.63"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了可靠地做到这一点,您必须使用状态来跟踪您是否在字符串内。以下 perl 脚本应该可以工作:
To do this reliably, you have to use states to keep track of wether you are inside a string or not. The following perl script should work:
“让 shell 运行 Perl 脚本”算不算?
如果是这样,我会看看 Perl 的 Text::CSV 模块。您将有两个 CSV 句柄,一个用于读取
sep_char
属性设置为逗号(标准、默认)的文件,另一个用于使用sep_char
写入文件属性设置为管道。工作脚本 示例
输入
示例输出
Does 'having shell run a Perl script' count?
If so, I'd look at Perl's Text::CSV module. You'd have two CSV handles, one for reading the file with the
sep_char
attribute set as comma (the standard, default), the other for writing the file with thesep_char
attribute set as pipe.Working script
Sample input
Sample output
如果文件中没有其他逗号,则可以使用:
如果逗号仅在
""
之间,则:工作原理如下:
如果您仍然可以有像
","
在你的输入中并且不想改变它,那么它会变得有点复杂,我认为:)使用专用模块的Python的另一种解决方案,可能在安全性和所需代码方面是最好的:
安全且简单。您可以轻松地将其调整为其他格式,参数相当简单。
If you have no other commas in your file, you can use:
If the commas are only between the
""
s, then:Works like this:
If you can still have an quoted-expression like
","
in your input and don't want to change that, then it gets a bit more complicated, I think :)Another solution with Python using a dedicated module, probably best in terms of safety and code needed:
Safe and simple. You can tweak this for other formats easily, the parameters are fairly straightforward.
Ruby 的 CSV 库在 1.9 中被替换为 FasterCSV;在早期版本中,您可以使用
fastercsv
gem。Ruby's CSV library was replaced with FasterCSV in 1.9; in earlier versions you can use the
fastercsv
gem.我遇到了同样的问题,我没有任何完美的解决方案,所以我尝试如下:
这将为传递给 shell 脚本的目录下的所有文件创建管道分隔的文件。这还处理列中具有额外逗号的转义字符。
I had same issue I did not any perfect solution so I tried as below:
This will create pipe-delimited files for all files under the directory passed to the shell script. This also handles the escape character which is having extra comma in a column.