Linux Shell Programming---Print rows in reverse order

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.