Three methods for Shell to read files by row,

Source: Internet
Author: User

Three methods for Shell to read files by row,

There are many methods for Shell to read files by row. The following three methods are written:

Statement 1:

#! /Bin/bash # Description: # while loop read Operations # Author: Standalone # version: 1.0 # Creation Time: 04: 08: 52 PATH =/bin:/sbin: /usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATHwhile read linedo echo $ linedone <filename (file to be read)

Statement 2:

#! /Bin/bash # Description: # cat Post-read Operations # Author: lone boat # version: 1.0 # Creation Time: 04: 08: 52 PATH =/bin:/sbin: /usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATHcat filename (file to be read) | while read linedo echo $ linedone

Statement 3:

#! /Bin/bash # Description: # for Loop read operations, which are different from while read Operations # Author: lone boat # version: 1.0 # Creation Time: 04: 08: 52 PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATHfor line in 'cat filename (file to be read) 'do echo $ linedone

Note: for Loop read row operations, which are different from while read rows

While is completely read by line, no matter how many paragraphs in the line;

For is read by row. If the text in the row contains spaces, it is read separately, that is, one string is read at a time.

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.