0
0

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 3 years have passed since last update.

テキストファイルの任意の文字列以降の行をすべて出力したい

Posted at

以下のようなテキストファイルに対して、任意の文字列以降の行をすべて出力したい

qiita.txt
aaa
bbb
ccc
ddd
eee
fff
ggg

例えば、"ccc" 以降を抽出したい場合は以下のようにgrepをするととりあえず可能

test.sh
# !/bin/bash

STR="ccc"
grep -A 1000 $STR qiita.txt

上記の記載だと、テキストファイルが1000行を超えるとファイルの最後が出力されなくなるので、(当たり前ですが、、)
以下のようにsedを使うとよい。

test.sh
# !/bin/bash

STR="ccc"
cat qiita.txt | sed -n '/'$STR'/,$p'

結果は以下の通りになる

$ bash test.sh
ccc
ddd
eee
fff
ggg

参考

sedコマンドの条件式に変数を使う場合に注意すること
今日のsed: 任意の文字列以降の行をすべて出力したい

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?