LoginSignup
5
0

More than 5 years have passed since last update.

カレントディレクトリにあるテキストファイルの、冒頭、末尾をそれぞれ指定行数だけ表示する

Last updated at Posted at 2017-09-01

次のようなファイルがある時

$ cat 1.txt
1-1
1-2
1-3
1-4
1-5
1-6
1-7
1-8
1-9
$ cat 2.txt
2-1
2-2
2-3
2-4
2-5
2-6
2-7
2-8
2-9
$ cat 3.txt
3-1
3-2
3-3
3-4
3-5
3-6
3-7
3-8
3-9

次のコマンドで、各ファイルの冒頭と末尾の指定行数を表示させる

$ find ./* -type f | xargs -I{} sh -c "(echo -e '{}\n-'; head -2; echo -e '---'; tail -2;echo -e '---------\n---------') < {}"
./1.txt
-
1-1
1-2
---
1-8
1-9
---------
---------
./2.txt
-
2-1
2-2
---
2-8
2-9
---------
---------
./3.txt
-
3-1
3-2
---
3-8
3-9
---------
---------

参考記事

find 結果それぞれにパイプ・リダイレクトを含むコマンドを適用する
Linux > Link > head / tail > (head; tail) < file.txt > 先頭と最後を同時表示

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