如何在sed中使用标签连接两行

How to concatenate two lines using label in sed

我有一个问题,是否有某种方法可以使用 sed 检测行尾。 Bcs 仅当行尾以负号结尾时我才需要连接两行 - 否则不需要。

sed -e :a -e '/,$/N; s/-\n*/new_line/; ta' test.txt (that is only what i have and i need to substite new_line for actualy new line)

如果文件看起来像那样

Here is a random sente-
nc and if random sentec ended with minus it is better to concatenate.

结果

Here is a random sentece and if random sentec ended with minus it is better to concatenate.

这可能适合您 (GNU sed):

sed '/-$/{N;s/-\n//}' file

如果该行以 - 结尾,追加下一行并删除第一行的最后一个字符和随后的换行符。

以下是在任何版本中 sed 的操作方法:

cat file

foo bar_-
Here is a random sente-
nce and if random sentec ended with minus it is better to concatenate.

# and use sed as
sed -e :a -e '/-$/{N;s/-\n//g;ta' -e '}' file

foo bar_Here is a random sentence and if random sentec ended with minus it is better to concatenate.