用新行替换带有新行的字符串和另一个字符串

时间:2017-08-06 19:32:01

标签: bash macos perl sed command-line

我需要在Mac OS下从命令行运行它 - 我有多种模式。

来自字符串:

</p>
<p>

要字符串:

</span>
<div>

我试过这个:

perl -pe 's:</p>\n<p>:</span>\n<div>:g'

但在换行符<p>

之后的文字失败了

3 个答案:

答案 0 :(得分:4)

要搜索多行字符串,您不必一次只读一行。

尝试:

perl -0777 -pe 's:</p>\n<p>:</span>\n<div>:g'

请参阅-0

答案 1 :(得分:1)

丑陋,但确实有效:

sed '/^<\/p>$/{N;/<p>/{s/<\/p>\(\n\)<p>/<\/span>\1<div>/;p;d;};P;D;}'

答案 2 :(得分:0)

Try this another solution, suppose if you don't want to slurp the whole file at a time,

perl -pe '$_.=<> if(/^<\/p>/); s:</p>\n<p>:</span>\n<div>:' input.txt

@Sinan Ünür上的按钮下推效果提到了他的评论,根据评论,如果当前行有<\p>,则需要捕获下一行。

因此,上面的代码捕获下一行并将字符串与$_$_已经拥有当前行数据)连接起来,然后根据需要替换该模式。然后无需在正则表达式中使用g修饰符。

相关问题