3 ways for the shell to read a file by line

Source: Internet
Author: User

Method 1:while the most efficient and most commonly used method in the loop.

While read line
Do
Echo $line
Done < filename

Note: This is done at the end of the file, as if it were done and then read the file in the same way.

Method 2: Pipe method: Cat $FILENAME | While Read line

cat filename | while the read line
Do
Echo $line
Done

Note: When a pipeline is encountered, the output of the command on the left side of the pipeline is entered as input to the command on the right side of the pipeline.

Method 3 for Loop.

For line in ' cat filename '
Do
Echo ${line}
Done

Note: This is a way to read the contents of a file through a for loop rather than everyone is familiar with, here not much to say.

In each method, the For statement is the most efficient and the first is the most efficient when reading and writing files in a while loop.

For row-by-line reading and while progressive reading are different, such as:

$ cat T.txt
1111
2222
3333 4444 555

$ Cat T.txt | while read line; Do Echo ${line}; Done
1111
2222
3333 4444 555

$ for line in ' Cat t.txt '; Do Echo ${line}; Done
1111
2222
3333
4444
555

3 ways for the shell to read a file by line

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.