删除匹配的第n行,直到awk / sed / grep中的空白行

时间:2010-12-16 14:51:31

标签: awk sed grep line

我需要从匹配到下一个空白行的文件中删除第n个匹配行(即从第n个匹配开始的一行空白行分隔文本)。

4 个答案:

答案 0 :(得分:2)

这将删除一个以第四个空白行开头的空白行开头和结尾的文本块。它还会删除那些分界线。

sed -n '/^$/!{p;b};H;x;/^\(\n[^\n]*\)\{4\}/{:a;n;/^$/!ba;d};x;p' inputfile

更改第一个/^$/以更改开始匹配。更改第二个以更改结束匹配。

鉴于此输入:

aaa
---
bbb
---
ccc
---
ddd delete me
eee delete me
===
fff
---
ggg

此版本的命令:

sed -n '/^---$/!{p;b};H;x;/^\(\n[^\n]*\)\{3\}/{:a;n;/^===$/!ba;d};x;p' inputfile

会给出结果:

aaa
---
bbb
---
ccc
fff
---
ggg

修改

我从上面的b命令中删除了无关的sed指令。

这是评论版:

sed -n '      # don't print by default
  /^---$/!{   # if the input line doesn't match the begin block marker
    p;        # print it
    b};       # branch to end of script and start processing next input line
  H;          # line matches begin mark, append to hold space
  x;          # swap pattern space and hold space
  /^\(\n[^\n]*\)\{3\}/{    # if what was in hold consists of 3 lines
                           # in other words, 3 copies of the begin marker
    :a;       # label a
    n;        # read the next line
    /^===$/!ba;    # if it's not the end of block marker, branch to :a
    d};       # otherwise, delete it, d branches to the end automatically
  x;          # swap pattern space and hold space
  p;          # print the line (it's outside the block we're looking for)
' inputfile   # end of script, name of input file

任何明确的模式都应该适用于开始和结束标记。它们可以相同也可以不同。

答案 1 :(得分:1)

perl -00 -pe 'if (/pattern/) {++$count == $n and $_ = "$`\n";}' file

-00是以“段落”模式读取文件(记录分隔符是一个或多个空行)

$`是Perl的“prematch”特殊变量(匹配模式前面的文字)

答案 2 :(得分:0)

在AWK中

/m1/  {i++};

(i==3)  {while (getline temp > 0 && temp != "" ){}; if (temp == "") {i++;next}};

{print}  

改变这个:

m1 1
first

m1 2
second

m1 3
third delete me!

m1 4
fourth

m1 5
last

进入这个:

m1 1
first

m1 2
second

m1 4
fourth

m1 5
last  

删除“m1”的第三个块...

Running on ideone here

HTH!

答案 3 :(得分:0)

强制性的awk脚本。只需将n=2更改为您的第n个匹配项。

n=2; awk -v n=$n '/^HEADER$/{++i==n && ++flag} !flag; /^$/&&flag{flag=0}' ./file

输入

$ cat ./file
HEADER
line1a
line2a
line3a

HEADER
line1b
line2b
line3b

HEADER
line1c
line2c
line3c

HEADER
line1d
line2d
line3d

输出

$ n=2; awk -v n=$n '/^HEADER$/{++i==n&&++flag} !flag; /^$/&&flag{flag=0}' ./file
HEADER
line1a
line2a
line3a

HEADER
line1c
line2c
line3c

HEADER
line1d
line2d
line3d