有没有办法使用sed替换除引号中的逗号之外的所有逗号
# Director, Movie Title, Year, Comment
Ethan Coen, No Country for Old Men, 2007, none
Ethan Coen, "O Brother, Where Art Thou?", 2000, none
就像在这里,引号外的逗号必须替换为|.
Ethan Coen| "O Brother, Where Art Thou?"| 2000| none
我确实尝试过这个命令
sed -e 's/,(?=(?:[^"]*"[^"]*")*[^"]*$)/|/g',
首先匹配这些字符串,把我收到 RE 错误:重复运算符操作数无效
我不知道如何将 re 转换为 shell 和正则表达式的全新内容,我将不胜感激
回答
您可以尝试使用此gnu awk命令FPAT使用自定义正则表达式拆分字段:
awk -v OFS='|' -v FPAT=' *"[^"]*"|[^",]+' '{$1=$1} 1' file
# Director| Movie Title| Year| Comment
Ethan Coen| No Country for Old Men| 2007| none
Ethan Coen| "O Brother, Where Art Thou?"| 2000| none
- Glad to know it worked out, [you may mark the answer as accepted by clicking on **tick mark** on top-left of this answer](http://meta.stackexchange.com/a/5235/160242)