4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

パターンにマッチする行と、それに続く一定の行数を取り除く

Posted at
awk 'BEGIN{n=0} /PATTERN/{n=NR+NUM} (NR>n){print}'

grep -vgrep -A NUM の合わせ技をやりたかったのですが、思ったようにできなかったので。

例)

$ cat sample
     1	01 ABC
     2	   DEF
     3	   GHI
     4	   JKL
     5	02 ABC
     6	   DEF
     7	   GHI
     8	   JKL
     9	03 ABC
    10	   DEF
    11	   GHI
    12	   JKL
    13	04 ABC
    14	   DEF
    15	   GHI
    16	   JKL
    17	05 ABC
    18	   DEF
    19	   GHI
    20	   JKL

03 にマッチする行と,それに続く3行を取り除きます。

$ cat sample | awk 'BEGIN{n=0} /03/{n=NR+3} (NR>n){print}'
     1	01 ABC
     2	   DEF
     3	   GHI
     4	   JKL
     5	02 ABC
     6	   DEF
     7	   GHI
     8	   JKL
    13	04 ABC
    14	   DEF
    15	   GHI
    16	   JKL
    17	05 ABC
    18	   DEF
    19	   GHI
    20	   JKL

BEGINブロックはなくても・・・、でも個人的には付ける派です。
/03/ のブロックと (NR>n) のブロックを入れ替えると結果が変わります。

grep -Bgrep -Cgrep -v の合わせ技は・・・どうやったらいいんでしょう?

4
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?