LoginSignup
0
0

More than 5 years have passed since last update.

ShellScriptで先頭や末尾を除く

Posted at

シェルを書いてるとheadtailコマンドで絞り込むことが良くあるんですが、先頭から数行除く、末尾から数行除くを毎度忘れるのでまとめておきます。

結論

head -n -N # 末尾からN行除いて表示
tail -n +N # 先頭からN行除いて表示

参考というか、丸々持ってきてしまった感じですが

Linuxでファイルから先頭・末尾の数行を除外する 俺的備忘録 〜なんかいろいろ〜

元ファイル

test.text
1
2
3
4
5
6
7
8
9

先頭から数行表示(普通にhead)

普通に先頭3行を表示

$ head -n 3 test.text 
1
2
3

末尾から数行除いて表示(head -n -N)

以下の場合は末尾3行を除く

$ head -n -3 test.text 
1
2
3
4
5
6

末尾数行表示(普通にtail)

普通に末尾3行を表示

$ tail -n 3 test.text 
7
8
9

先頭から数行除いて表示(tail -n +N)

ちょっとややこしいのが、3行目から表示するという意味になること
3行除くではない

$ tail -n +3 test.text 
3
4
5
6
7
8
9

参考

Linuxでファイルから先頭・末尾の数行を除外する 俺的備忘録 〜なんかいろいろ〜

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