English 中文(简体)
替换两条新线,代之以双向指挥线。
原标题:replace two newlines to one in shell command line

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

我需要将以下两条新路线(如果有的话)替换为一条新线:MARKER。 结果是:

first line
second line MARKER
third line MARKER
other lines

many other lines

I tried sed :a;N;$!ba;s/MARKER /MARKER /g Fail.
sed is useful for single line replacements but has problems with newlines. It can t find

I tried perl -i -p -e s/MARKER /MARKER /g Fail.
This solution looks closer, but it seems that regexp didn t reacts to .

Is it possible to replace only after MARKER and not to replace other in the file?
I am interested in one-line-solution, not scripts.

最佳回答

我认为,你正走上正轨。 在一个多线方案中,你将把整个档案装入一个单体,并将这一替代材料投入:

s/MARKER

/MARKER
/g

将档案装入多线载体的trick计是将<条码><><>>/>/代码>列入<条码>。 该法典将一经执行,然后才阅读投入。

perl -i -pe  BEGIN{$/=undef} s/MARKER

/MARKER
/g  input
问题回答

由于你正在寻找两条新路线,你的解决办法就不可行。 没有这样的东西。 这里的解决办法是:

perl -ne print if !$m || !/^$/; $m = /MARKER$/;  infile > outfile

或内部:

perl -i~ -ne print if !$m || !/^$/; $m = /MARKER$/;  file

如果你重新把整个档案装上记忆中,你可以使用

perl -0777pe s/MARKER

/MARKER
/g;  infile > outfile

perl -0777pe s/MARKER
K
//g;  infile > outfile

如上所述,您可在网上使用<代码>-i~。 如果你不希望支持的话,删除<代码>~。

awk:

kent$  cat a
first line
second line MARKER

third line MARKER
other lines

many other lines

kent$  awk  BEGIN{RS="x034"} {gsub(/MARKER

/,"MARKER
");printf $0}  a
first line
second line MARKER
third line MARKER
other lines

many other lines
awk  
  marker { marker = 0; if (/^$/) next }
  /MARKER/ { marker = 1 }
  { print }
 

可在非常简单的<代码>上填写。

sed  /MARKER$/{n;/./!d} 

这或许有助于你:

 sed  /MARKER/,//{//!d} 

解释:

删除<代码>MARKER 保全MARKER之间的所有线。

Or:

sed  /MARKER/{n;N;//D} 

解释:

Read the next line after MARKER, then append the line after that. Delete the previous line if the current line is a MARKER line.





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签