用一个逗号替换多个逗号 - linux 命令

Replace multiple commas with a single one - linux command

这是我的 google csv 联系人(包含超过 1000 个联系人)的输出:

A-Tech Computers Hardware,A-Tech Computers,,Hardware,,,,,,,,,,,,,,,,,,,,Low,,,* My Contacts,,,,,,,,,Home,+38733236313,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

我需要一个 linux cli 命令来用单个逗号替换重复的逗号,所以我得到了这个:

A-Tech Computers Hardware,A-Tech Computers,Hardware,Low,* My Contacts,Home,+38733236313,

我通常在notepad++中做的是将“,”替换为“,”六次。

我试过:

cat googlecontacts.txt | sed -e 's/,,/,/g' -e 's/,,/,/g' -e 's/,,/,/g'  -e 's/,,/,/g' -e 's/,,/,/g' -e 's/,,/,/g' > google.txt

但是没用... 但是,当我在较小的文件(两行)上尝试时,它起作用了……:(

请帮忙!

假设你的线路在修改后仍然合规(不是问题的关注点)

sed 's/,\{2,\}/,/g' googlecontacts.txt > google.txt
  • 它将 , 中任何大于 1 的出现替换为单个 , 行上的任何位置
  • , 之间的任何 space 都被认为是正确的字段,因此未修改

在您的命令中,您需要递归更改字符,而不是重复执行多次相同的操作(总有可能发生严重错误), 像这样

cat googlecontacts.txt | sed ':a
# make your change
s/,,/,/g
# if change occur, retry once again by returning to line :a
t a' > google.txt

您需要 trsqueeze 选项:

tr -s ',' < yourFile

你可以看到它是这样运行的:

echo hello,,there,,,,I,have,,too,many,,,commas | tr -s ,
hello,there,I,have,too,many,commas

这可能适合您 (GNU sed):

sed 's/,,*/,/g' file

sed 's/,\+/,/g' file

谢谢@potong,您的解决方案满足了我的一个要求。我不得不更换 |符号在我的文件的第一行,并使用这个解决方案稍作改动。

sed -i "1s/|'*//g" ${filename} 

我无法添加评论,所以考虑将其作为答案发布。请见谅