"Shell script" line-by-row processing of text files

Source: Internet
Author: User

"Shell script" line-by-row processing of text files

The style files are often processed on a line-by-row basis, how to get each row of data in the shell, then process the row data, and finally read the next row of data, loop processing. There are several workarounds for the following:

1. Complete by the Read command.

The read command receives input from a standard input, or other file descriptor, and when it is entered, the Read command puts the data into a standard variable.

When reading a file with read, each call to the Read command reads the "one line" of text in the file.

When a file does not have a readable row, the read command exits with a non-0 status.

1 Cat Data.dat | awk ' {print $} ' 2 Cat Data.dat | awk ' for (i=2;i<nf;i++) {printf $i} printf "\ n"} ' the 1th line of code outputs each row of data in Data.dat, and the 2nd code outputs the data from column 2nd in each row. It is convenient to use the awk command if it is purely data or text files that are read and displayed by line. 3. Use the for var in file command to complete the Var in file to represent the variable var in the file loop value. The delimiter value is determined by $ifs. 1 for line in $ (cat Data.dat) 2 does 3 echo "File:${line}" 4 Done5 6 for line in ' Cat Data.dat ' 7 do 8 echo "File:${line}" 9 Don E If there are no spaces in each line of the input text, then line loops the value in the input text by the newline character delimiter. If the input text includes spaces or tabs, it is not a newline read, and line takes the value in the input text by a space separator or tab or newline traits classes ring. You can achieve progressive reading by setting the IFS to line breaks. The default values for IFS are: white space (including: spaces, tabs, newline characters).


This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1838537

Shell script processes text files line by row

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.