如何在Linux上向后读取文件?

时间:2013-02-17 21:03:29

标签: linux cat

我知道我可以使用cat在Linux上从头到尾打印文件中的所有内容。 有没有办法向后做(最后一行)?

3 个答案:

答案 0 :(得分:10)

是的,您可以使用“tac”命令。

来自man tac:

Usage: tac [OPTION]... [FILE]...
Write each FILE to standard output, last line first.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --before             attach the separator before instead of after
  -r, --regex              interpret the separator as a regular expression
  -s, --separator=STRING   use STRING as the separator instead of newline
      --help     display this help and exit
      --version  output version information and exit

答案 1 :(得分:6)

sed '1!G;h;$!d' file

sed -n '1!G;h;$p' file

perl -e 'print reverse <>' file

awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file

答案 2 :(得分:5)

tac是一种方式,但并非所有Linux都可以默认使用。

awk可以这样做:

awk '{a[NR]=$0}END{for(i=NR;i>=1;i--)print a[i]}' file