LoginSignup
18
12

More than 5 years have passed since last update.

今日のsed: 任意の文字列以降の行をすべて出力したい

Last updated at Posted at 2016-03-03

やりたいこと

こういうファイルがあったとして、bar 以降の行だけ出力したい。

qiita.txt
hoge
foo
bar
buzz
piyo

一度しか出現しないケース

それsedで出来るよ。

cat /tmp/qiita.txt | sed -n '/bar/,$p'
bar
buzz
piyo

これでも良さそうだけど、超絶カッコ悪い。

cat /tmp/qiita.txt | tac | sed '/bar/q' | tac
bar
buzz
piyo

2回以上出現するケース

2回以上出現するケースで使い分けたらいいよ。

qiita2.txt
hoge
foo
bar
buzz
piyo
hoge
foo
bar
buzz
piyo
  • 初出以降全部出力
cat /tmp/qiita2.txt | sed -n '/bar/,$p'
bar
buzz
piyo
hoge
foo
bar
buzz
piyo
  • 最後の出現以降全部出力
cat /tmp/qiita2.txt | tac | sed '/bar/q' | tac
bar
buzz
piyo
18
12
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
18
12