![使用 sed 命令替换所有出现的双引号,除非其后跟或前面有逗号](https://linux22.com/image/100373/%E4%BD%BF%E7%94%A8%20sed%20%E5%91%BD%E4%BB%A4%E6%9B%BF%E6%8D%A2%E6%89%80%E6%9C%89%E5%87%BA%E7%8E%B0%E7%9A%84%E5%8F%8C%E5%BC%95%E5%8F%B7%EF%BC%8C%E9%99%A4%E9%9D%9E%E5%85%B6%E5%90%8E%E8%B7%9F%E6%88%96%E5%89%8D%E9%9D%A2%E6%9C%89%E9%80%97%E5%8F%B7.png)
我想将文件中所有出现的 替换为 ,除非它们"
后面或前面有逗号 ( ) 或者它们位于行的开头^"
"
,
IE -
"InvoiceA","Doe,John",12/01/2016,200,"Comment on Invoice: "Customer X purchased item Z from store C named "Abc" on "03/04/2016""
应转换为 -
"InvoiceA","Doe,John",12/01/2016,200,"Comment on Invoice: ^"Customer X purchased item Z from store C named ^"ABC^" on ^"03/04/2016^""
答案1
我假设您还想在行尾保留引号字符不变(如您的示例所示)。你可以这样做
sed 's/\([^,]\)"\([^,]\)/\1^"\2/g'
这将查找三个连续字符的字符串,如下所示:
- 除逗号 ( ) 之外的字符
,
, - 双引号 (
"
),以及 - 除逗号以外的字符,
并将它们替换为
- 第一个角色,
^"
, 和- 第三个角色。
这不适用于多个连续的引号字符;例如,
…200,"Purchase at store named "" on Tuesday"
PS 你知道你的例子有不平衡的报价吗?
"Comment on Invoice: "Customer X purchased item Z from store C named "Abc" on "03/04/2016""
↑ ↑ ↑ ↑ ↑