如何从上线到Shell中的第一行读取文件



我想阅读从最后一行到第一行的一行。例如:蚂蚁球猫最多50行我需要向后打印

解决方案1st: 将使用tac

tac Input_file

解决方案2:,以防系统中没有tac

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

解决方案3:使用另一个sed解决方案:

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

解决方案4:也使用awk

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

解决方案5th: perl解决方案:

perl -e 'print reverse <>'  Input_file

使用 tac [file],就像 cat一样,但向后打印行。

来自TAC手册页:

名称

   tac - concatenate and print files in reverse

摘要

   tac [OPTION]... [FILE]...
$ echo -e 1\n2 |
awk '{b=$0 (b==""?"":ORS b)}END{print b}'
2
1

解释:

$ awk '{b=$0 (b==""?"":ORS b)}  # buffer records to the beginning of b
END{print b}'                   # after all records were buffered print b

最新更新