Bash 如何从shell的最后一行到第一行读取文件?

Bash 如何从shell的最后一行到第一行读取文件?,bash,shell,Bash,Shell,我想从最后一行读到第一行。 例如: 蚂蚁 球 猫 最多50行 我需要向后打印它使用tac[file],这类似于cat,但会向后打印行 从tac手册页面: 名字 概要 tac [OPTION]... [FILE]... 解决方案1:将使用tac: tac Input_file 解决方案2:如果您的系统中没有tac: sed '1!G;h;$!d' Input_file 解决方案3:还有另一个sed解决方案: sed -n '1!G;h;$p' Input_file 第四种解决方案:

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

使用
tac[file]
,这类似于
cat
,但会向后打印行

从tac手册页面:

名字

概要

   tac [OPTION]... [FILE]...
解决方案1:将使用
tac

tac Input_file
解决方案2:如果您的系统中没有
tac

sed '1!G;h;$!d'  Input_file
解决方案3:还有另一个
sed
解决方案:

sed -n '1!G;h;$p' Input_file
第四种解决方案:也使用
awk

awk '{a[FNR]=$0} END{for(i=FNR;i>=1;i--){print a[i]}}'   Input_file
解决方案5:
perl
解决方案:

perl -e 'print reverse <>'  Input_file
perl-e“打印反向”输入文件
解释:

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

tac
是否足够(
mantac
)?即使您的代码可能是OP问题的有效解决方案,也建议您描述代码片段的用途,而不是仅添加代码。
$ 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