在 shell 命令行中将两个换行符替换为一个
关于将多个换行符替换为一个换行符存在很多问题,但没有人为我工作。
我有一个文件:
first line
second line MARKER
third line MARKER
other lines
many other lines
我需要将 MARKER
之后的两个换行符(如果存在)替换为一个换行符。结果文件应该是:
first line
second line MARKER
third line MARKER
other lines
many other lines
我尝试 sed ':a;N;$!ba;s/MARKER\n\n/MARKER\n/g'
失败。sed
对于单行替换很有用,但对于换行符有问题。它找不到\n\n
我尝试了perl -i -p -e 's/MARKER\n\n/MARKER\n/g'
失败。< br> 这个解决方案看起来更接近,但似乎正则表达式没有对 \n\n
做出反应。
是否可以只替换MARKER
之后的\n\n
而不替换文件中的其他\n\n
?
我对单行解决方案感兴趣,而不是脚本。
There are lot of questions about replacing multi-newlines to one newline but no one is working for me.
I have a file:
first line
second line MARKER
third line MARKER
other lines
many other lines
I need to replace two newlines (if they exist) after MARKER
to one newline. A result file should be:
first line
second line MARKER
third line MARKER
other lines
many other lines
I tried sed ':a;N;$!ba;s/MARKER\n\n/MARKER\n/g'
Fail.sed
is useful for single line replacements but has problems with newlines. It can't find \n\n
I tried perl -i -p -e 's/MARKER\n\n/MARKER\n/g'
Fail.
This solution looks closer, but it seems that regexp didn't reacts to \n\n
.
Is it possible to replace \n\n
only after MARKER
and not to replace other \n\n
in the file?
I am interested in one-line-solution, not scripts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(7)
您的 Perl 解决方案不起作用,因为您正在搜索包含两个换行符的行。没有这样的事情。这是一种解决方案:
perl -ne'print if !$m || !/^$/; $m = /MARKER$/;' infile > outfile
Or in-place:
perl -i~ -ne'print if !$m || !/^$/; $m = /MARKER$/;' file
如果您同意将整个文件加载到内存中,您可以使用
perl -0777pe's/MARKER\n\n/MARKER\n/g;' infile > outfile
or
perl -0777pe's/MARKER\n\K\n//g;' infile > outfile
如上所述,您可以使用 -i~
进行就地编辑。如果您不想进行备份,请删除 ~
。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我认为你走在正确的道路上。在多行程序中,您可以将整个文件加载到单个标量中并对其运行此替换:
让单行程序将文件加载到多行字符串中的技巧是设置
$/
在BEGIN
块中。该代码将在读取输入之前执行一次。I think you were on the right track. In a multi-line program, you would load the entire file into a single scalar and run this substitution on it:
The trick to getting a one-liner to load a file into a multi-line string is to set
$/
in aBEGIN
block. This code will get executed once, before the input is read.