输入:
"stuff blah mostuff" "stuff and blahagain" "stuff yeablah"
所需的输出:"stuff blah mostuff","stuff and blahagain","stuff yeablah"
如何使用awk执行此操作?
答案 0 :(得分:3)
$ awk -F'" "' -v OFS='","' '{$1=$1}1' file
"stuff blah mostuff","stuff and blahagain","stuff yeablah"
但由于这是单行上的简单替换,因此它比awk更适合sed:
$ sed -r 's/("[^"]+") /\1,/g' file
"stuff blah mostuff","stuff and blahagain","stuff yeablah"
$ sed -r 's/" "/","/g' file
"stuff blah mostuff","stuff and blahagain","stuff yeablah"
如果您的真实数据比您显示的更复杂(例如,可以嵌入转义的引号或字符串或嵌入的换行符之间的不同间距或仅包含空白字符或...的字符串),请编辑您的问题以显示一些真正具有代表性的投入和预期产出。
答案 1 :(得分:1)
只需将" "
替换为","
$ echo '"stuff blah mostuff" "stuff and blahagain" "stuff yeablah"' | awk '{gsub(/" "/,"\",\"")}1'
"stuff blah mostuff","stuff and blahagain","stuff yeablah"
通过sed,
$ echo '"stuff blah mostuff" "stuff and blahagain" "stuff yeablah"' | sed 's/"[[:blank:]]"/","/g'
"stuff blah mostuff","stuff and blahagain","stuff yeablah"
答案 2 :(得分:0)
一种不同的方法,
awk -F\" -v OFS=\" '{for (i=3; i<=NF-2; i+=2) $i=","}1' file