[英]sed - remove line break if line does not end on \"
I have a tsv.-file and there are some lines which do not end with an '"'. So now I would like to remove every line break which is not directly after an '"'. 我有一个tsv。文件,有些行不以'“'结尾,所以现在我想删除不是直接在'”'之后的所有换行符。 How could I accomplish that with sed?
我怎么能用sed做到这一点? Or any other bash shell program...
或任何其他bash shell程序...
Kind regards, Snafu 亲切的问候,斯纳夫
This sed
command should do it: 此
sed
命令应执行以下操作:
sed '/"$/!{N;s/\n//}' file
It says: on every line not matching "$
do: 它说:在不匹配的每一行上,
"$
do:
Example: 例:
$ cat file.txt
"test"
"qwe
rty"
foo
$ sed '/"$/!{N;s/\n//}' file.txt
"test"
"qwerty"
foo
To elaborate on @Lev's answer, the BSD (OSX) version of sed
is less forgiving about the command syntax within the curly braces -- the semicolon command separator is required for both commands: 为了详细说明@Lev的答案,
sed
的BSD(OSX)版本对花括号中的命令语法不太宽容-这两个命令都需要使用分号命令分隔符:
sed '/"$/!{N;s/\n//;}' file.txt
per the documentation here -- an excerpt: 根据此处的文档 -摘录:
Following an address or address range, sed accepts curly braces '{...}' so several commands may be applied to that line or to the lines matched by the address range.
在地址或地址范围之后,sed接受大括号“ {...}”,因此可以在该行或该地址范围匹配的行上应用几个命令。 On the command line, semicolons ';'
在命令行上,分号“;” separate each instruction and must precede the closing brace.
分开每条指令,并且必须在大括号之前。
give this awk one-liner a try: 试试这个awk一线尝试:
awk '{printf "%s%s",$0,(/"$/?"\n":"")}' file
test 测试
kent$ cat f
"foo"
"bar"
"a long
text with
many many
lines"
"lalala"
kent$ awk '{printf "%s%s",$0,(/"$/?"\n":"")}' f
"foo"
"bar"
"a longtext withmany manylines"
"lalala"
This might work for you (GNU sed): 这可能对您有用(GNU sed):
sed ':a;/"$/!{N;s/\n//;ta}' file
This checks if the last character of the pattern space is a "
and if not appends another line, removes a newline and repeats until the condition is met or the end-of-file is encountered. 这将检查模式空间的最后一个字符是否为
"
,如果不是,则追加另一行,删除换行符并重复执行,直到满足条件或遇到文件结尾为止。
An alternative is: 一种替代方法是:
sed -r ':a;N;s/([^"])\n/\1/;ta;P;D' file
The mechanism is left for the reader to ponder. 该机制留给读者思考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.