4
3

More than 3 years have passed since last update.

[Shell]任意の文字列以降の行をすべて出力する

Last updated at Posted at 2020-03-30

目的

以下の出力で、ccc以降を表示したい。

hoge.txt
aaa
bbb
ccc
ddd
eee

grepのAオプションを使う

以下のコマンドでccc出現以降の100行を表示できます。

cat hoge.txt | grep -A 100 ccc
出力
ccc
ddd
eee

また、BSD系のgrepに限りAオプションに-1を指定することが可能です。-1を指定すると、cccの出現から行末まで出力されます。

sedを使用する

以下の記事の通りsedを使用しても実現可能です。
こちらは、行数を指定が不要のため、出力が何行になっても使用可能です。
今日のsed: 任意の文字列以降の行をすべて出力したい

cat hoge.txt | sed -n '/ccc/,$p'
4
3
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
4
3