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 1 year has passed since last update.

【Linux】ファイルの内容を逆順で出力する方法

Posted at

tacコマンド

tacコマンドはファイルの行を逆にして出力します。

$ cat sample.txt
1 abcde
2 abcde
3 abcde
4 abcde
5 abcde

$ tac sample.txt
5 abcde
4 abcde
3 abcde
2 abcde
1 abcde

複数のファイルのそれぞれの行を逆にして連結する場合には次のように実行します。

sample.txt
$ cat sample.txt example.txt
1 abcde
2 abcde
3 abcde
4 abcde
5 abcde
a 12345
b 12345
c 12345
d 12345
e 12345

$ tac sample.txt example.txt
5 abcde
4 abcde
3 abcde
2 abcde
1 abcde
e 12345
d 12345
c 12345
b 12345
a 12345

複数のファイルを連結したうえで行を逆にして出力するには次のようにします。

$ cat sample.txt example.txt | tac
e 12345
d 12345
c 12345
b 12345
a 12345
5 abcde
4 abcde
3 abcde
2 abcde
1 abcde

revコマンド

revコマンドはファイルの行の文字列を逆にして出力します。

$ rev sample.txt
edcba 1
edcba 2
edcba 3
edcba 4
edcba 5

ファイル全体を末尾から先頭に向かって出力するには次のようにします。

$ tac sample.txt | rev
edcba 5
edcba 4
edcba 3
edcba 2
edcba 1
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?