我有两行:
abc,efg,"hij
kl","dfds,f"
我想删除第一行末尾的换行符,但前提是它后面没有"
. 也就是说,我想要这个结果:
echo 'abc,efg,"hij
kl","dfds,f"' | xxxxxxxxxx - > abc,efg,"hijkl","dfds,f"
但
abc,efg,"hij"
"kl","dfds,f"
应该保持为 2 行本身
这个 awk one-liner 可以帮助你:
awk '/[^"]$/{printf "%s",$0;next}7'
使用您的数据进行测试:
kent$ echo 'abc,efg,"hij
kl","dfds,f"'|awk '/[^"]$/{printf "%s",$0;next}7'
abc,efg,"hijkl","dfds,f"
如果您有多条 (>2) 不以 结尾的连续线,则此单行有效"
,例如:
kent$ echo 'abc,efg,"hij
abc,efg,"hij
abc,efg,"hij
abc,efg,"hij
kl","dfds,f"'|awk '/[^"]$/{printf "%s",$0;next}7'
abc,efg,"hijabc,efg,"hijabc,efg,"hijabc,efg,"hijkl","dfds,f"
操纵的ORS
让你做到这一点。
cat file
abc,efg,"hij
kl","dfds,f"
"this should be on its own line","but
this shouldn't"
awk '/[^"]$/{ORS=""} !/[^"]$/{ORS="\n"} 1' file
abc,efg,"hijkl","dfds,f"
"this should be on its own line","but this shouldn't"