如果没有后跟双引号,如何删除换行符(\ n)

时间:2013-10-05 00:14:33

标签: bash shell

我有两行:

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行本身

2 个答案:

答案 0 :(得分:1)

这个awk单行可以帮助你:

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"

答案 1 :(得分:0)

操纵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"