Regex 替换与正则表达式匹配的行的换行符

Regex 替换与正则表达式匹配的行的换行符,regex,perl,sed,Regex,Perl,Sed,我有这样一个文件: 72810 82:28:1 this is text hello world 72810 82:28:12 some more text hello wolrd again 我只需要替换包含72810的行中的换行符* 但有一个陷阱:我尝试了以下方法: sed's/\n//g'myfile.txt 但这不起作用 我也尝试过: cat myfile.txt | perl-p-e's/72810.*\n/72810/g' 但这并不保存完整的原始文本(在本例中为72810 82:

我有这样一个文件:

72810 82:28:1
this is text
hello world

72810 82:28:12
some more text
hello wolrd again
我只需要替换包含72810的行中的换行符* 但有一个陷阱:我尝试了以下方法:

sed's/\n//g'myfile.txt

但这不起作用

我也尝试过:

cat myfile.txt | perl-p-e's/72810.*\n/72810/g'

但这并不保存完整的原始文本(在本例中为72810 82:28:1或72810 82:28:12)

我需要做什么才能使文件看起来像:

72810 82:28:1 this is text
hello world

72810 82:28:12 some more text
hello world again
顺便说一句,我正在使用solaris。

试试这个:

perl -ne 'if (m/72810/) {s/\n/ /; print} else { print }' input.txt
或者更简短的版本:

perl -pe 's/\n/ / if m/72810/' input.txt
您可以使用
-i
选项就地编辑
input.txt

尝试以下操作:

perl -ne 'if (m/72810/) {s/\n/ /; print} else { print }' input.txt
或者更简短的版本:

perl -pe 's/\n/ / if m/72810/' input.txt

您可以使用
-i
选项就地编辑
input.txt

在包含测试字符串的所有行上替换换行符似乎是最简单的。像这样

perl -pe "s/\n/ / if /72810/" myfile.txt
输出

72810 82:28:1 this is text
hello world

72810 82:28:12 some more text
hello wolrd again

在包含测试字符串的所有行上替换换行符似乎是最简单的。像这样

perl -pe "s/\n/ / if /72810/" myfile.txt
输出

72810 82:28:1 this is text
hello world

72810 82:28:12 some more text
hello wolrd again
这可能适用于您(GNU-sed):

这可能适用于您(GNU-sed):


可能的副本我已经搜索了这个特定的问题,但没有结果。这个问题涉及到只能替换特定匹配上的换行符。我已经搜索了这个特定问题,但没有结果。这个问题涉及到只能在特定的匹配项上替换换行符