Transfer from http://www.cnblogs.com/dwdxdy/archive/2012/07/25/2608816.html
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 | While read line2 Do3 echo ' File:${line} ' 4 done5 6 while read Line7 does 8 echo "File:${line}" 9 Done < Data.dat
2. Use the awk command to complete
Awk is an excellent text processing tool that provides extremely powerful functionality.
Using awk to read each row of data in a file, you can do some processing on each row of data, and each column of data in each row of data can be processed separately.
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 the 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 for Var in file represents 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:${lin e} "9 done
If there are no spaces in each line of the input text, the 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).
[Reprint] "Shell script" line-by-row processing of text files