使用awk命令用逗号替换双引号csv文件中的管道

时间:2017-06-15 08:11:49

标签: linux bash csv awk

我是awk命令的新手,我有包含管道的数据,我想用逗号替换这些管道。以下是数据样本

"PT3QB789TSUIDF371261","THE TORONTO|DOMINION BANK","2016-05-24T00:36:42Z","ANZ BANK NEW| ZEALAND LIMITED","HZSN7FQBPO5IEWYIGC72","MAS|CA.ON.OSC|ASIC*|AAAA"
"PT3QB789TSUIDF371261","ABC|XYZ|123","2016-05-24T00:36:42Z","ANZ BANK NEW ZEALAND LIMITED","HZSN7FQBPO5IEWYIGC72","MAS|CA.ON.OSC|ASIC*"

我的输出应该看起来像

"PT3QB789TSUIDF371261","THE TORONTO,DOMINION BANK","2016-05-24T00:36:42Z","ANZ BANK NEW, ZEALAND LIMITED","HZSN7FQBPO5IEWYIGC72","MAS,CA.ON.OSC,ASIC*,AAAA"
"PT3QB789TSUIDF371261","ABC,XYZ,123","2016-05-24T00:36:42Z","ANZ BANK NEW ZEALAND LIMITED","HZSN7FQBPO5IEWYIGC72","MAS,CA.ON.OSC,ASIC*"

我尝试了以下对我不起作用的命令

awk -F"\"" 'BEGIN{OFS="\""}{for(i=1;i<=NF;++i){ if(i%2==0) gsub(/|/, ",", $i)}} {print $0}' file

请帮我解决awk命令。谢谢我的进步。

2 个答案:

答案 0 :(得分:1)

If you want to change ALL pipes to commas, either sed:

sed -i 's/|/,/g' file.txt

or tr:

tr \| , < file.txt > file1.txt

But... if you really want to use awk I'd try something like this (cannot test it right now):

awk -F\| 'BEGIN{OFS=","} {$1=$1; print}' file.txt > file1.txt

答案 1 :(得分:0)

如果您可以选择不使用awk,那么您可以试试这个 -

sed -i's / theOriginalChar / theNewChar / g'theFile.txt

如果你必须使用awk- 注意垂直条(|)允许正则表达式 所以使用|在您的示例中,您需要在|

之前添加\