"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