我知道我可以使用cat
在Linux上从头到尾打印文件中的所有内容。 有没有办法做到这一点(最后一行)?
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
是的,你可以使用“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
tac
是一种方法,但不是所有Linux上的默认可用。
awk可以这样做:
awk '{a[NR]=$0}END{for(i=NR;i>=1;i--)print a[i]}' file