sedを使用してファイルから1行を抽出する

sedを使用してファイルから1行を抽出する

sed「start」の入力ファイルを検索し、「next」を含む行を見つけて、次の行を表示するスクリプトを作成するにはどうすればよいですか?このような:

[user]$ cat test.txt
start
next
This line should print
Ignore this


[user]$ display.sed test.txt
This line should print

[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too

[user]$ display.sed test1.txt
This line should print

ベストアンサー1

範囲(最初からstartファイルの終わりまで)を使用して、dその範囲内で一致しないすべての行を削除できますnext。行が一致する場合は、n次の行を読んでp印刷してから、q次のように入力します。

sed -n '/start/,${
/next/!d;n;p;q
}' infile

あなたが本当に欲しいのは、次のものを含むファイルだと思いますdisplay.sed

#!/bin/sed -nf

/start/,${
/next/!d;n;p;q
}

おすすめ記事