Print rows in reverse order
Use awk or TAC can be done.
TAC The name of this command is actually written in reverse. Cat .
(1) , using TAC
The syntax for this command is as follows:
Tac File1file2 ...
It can also be used from stdin Read in:
$ seq 5 |tac
5
4
3
2
1
in the TAC in which \ n is the default row delimiter. But we can also use the- S " delimiter " option to specify their own delimiter.
(2) , using awk
Use awk are implemented in the following ways:
$ seq 9 |\
awk ' {lifo[nr]=$0}
End{for (lno=nr;lno>-1;lno--) {print Lifo[lno];}
}‘
in the Shell script, \ it is convenient to disassemble a single-line command into multiple rows.
thisawkThe script is very simple. We store each row in an associative array, using the row number as the array index (the line number isNRgiven) and finally byawkExecutionENDstatement block. In order to get the line number of the last line,{ }used in statement blocksLno=nr. Therefore, the script iterates from the last line to the first0rows, the rows stored in the array are printed in reverse order.
This article is from the "operation and maintenance of micro-letter" blog, please be sure to keep this source http://weixiaoxin.blog.51cto.com/13270051/1963689
Linux Shell Programming---Print rows in reverse order