LoginSignup
7
4

More than 5 years have passed since last update.

sedで特定行の次の行を削除する

Last updated at Posted at 2016-12-27

sedで検索文字列の次の行を消したい時に困ったので備忘録を残しておこうと思います。

環境

mac

方法

$ sed -e '/検索文字列/{n;d;}' ファイル名

例えば、

hoge.txt
aaa
bbb
ccc
ddd

に対して

$ sed -e '/bbb/{n;d;}' hoge.txt

上記コマンドを実行すると

aaa
bbb
ddd

この様な結果が得られます。

注意しないといけないのは、中括弧の中の最後のコマンドにもセミコロンをつけてあげないとextra characters at the end of d commandとエラーになります。

次の次の行

$ sed -e '/検索文字列/{n;n;d;}' ファイル名

-iオプション

-iをつけるとファイルが直接書き換えられます。

7
4
0

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
7
4