0

我有两行:

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

4

2 回答 2

1

这个 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"
于 2013-10-05T10:38:10.217 回答
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"
于 2013-10-05T11:05:51.940 回答