我想将文件中所有出现的 替换为 ,除非它们"
后面或前面有逗号 ( ) 或者它们位于行的开头^"
"
,
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""
↑ ↑ ↑ ↑ ↑