LoginSignup
2
1

More than 5 years have passed since last update.

grepで指定行数スキップする

Posted at

grepは指定バイト数スキップのオプション(-b, --byte-offset)は有るけど、指定行数スキップするオプションはない。
判り切っているヘッダを除外してgrepしたい場合など、以下の様に、tailを組み合わせて処理します。

tail -n +10 foo.txt | grep "bar"

tailの-nオプションは、tailい表示する行数を指定するオプションですが、「+10」の様に指定すると10行目以降を表示するという指定になります。

実行例
user@hostname ~
$ cat testdata.txt
1, 0
2, 2
3, 4
4, 6
5, 8
6, 0
7, 2
8, 4
9, 6
10, 8
11, 0
12, 2
13, 4
14, 6
15, 8
16, 0
17, 2
18, 4
19, 6
20, 8
21, 0
22, 2
23, 4
24, 6
25, 8

user@hostname ~
$ grep ", 8" testdata.txt
5, 8
10, 8
15, 8
20, 8
25, 8

user@hostname ~
$ tail -n +10 testdata.txt | grep ", 8"
10, 8
15, 8
20, 8
25, 8

user@hostname ~
$
2
1
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
2
1