格式为
的csv文件name age comments
John 12 Hello World,Example
Jack 14 Hello,World
如何使用|(管道)符号替换注释部分中的逗号。
逗号也可以出现在姓名,年龄和评论中。如何在文本中动态处理它?</ p>
答案 0 :(得分:2)
由于您似乎没有使用逗号作为字段分隔符,因此它就像
一样简单sed 's/,/|/g' file
仅在评论栏中替换:
while read name age comments; do echo "$name $age ${comments//,/|}"; done < file
答案 1 :(得分:0)
# any separator char but `/\.&[]` (or need to change the sed action), here `;`
Separator=";"
sed -e ":a
s/\(${Separator}[^${Separator}]*\),\([^${Separator}]*\)$/\1|\2/
ta" YourFile
在GNU sed上使用--posix
的Posix版本