Style files are often processed row by row. In Shell, how to obtain each row of data, then process the row of data, and finally read the next row of data can be processed cyclically. There are multiple solutions:
1. Run the READ command.
The read command receives standard input or input from other file descriptors. After the input is obtained, the READ command puts the data in a standard variable.
When you use read to read a file, each time you call the READ command, the "One Line" text in the file is read.
When the file has no readable rows, the READ command will exit in a non-zero state.
1 Cat Data. dat | While Read line 2 Do 3 Echo " File: $ {Line} " 4 Done 5 6 While Read line 7 Do 8 Echo " File: $ {Line} " 9 Done <Data. dat
2. Run the awk command to complete
Awk is an excellent text processing tool and provides extremely powerful functions.
Awk can be used to read data in each row of the file, and some processing can be performed on each row of data. It can also separately process each column of data in each row.
1 CatData. dat |Awk '{Print $0}'2 CatData. dat |Awk 'For (I = 2; I <NF; I ++) {printf $ I} printf "\ n "}'
1st rowsCodeOutput data in each row of data. dat, and code 2nd outputs data in each row after 2nd columns.
The awk command is convenient for reading and displaying data or text files by line.
3. Use the for VAR in file command to complete
For VaR in file indicates that the variable VAR is cyclically valued in file. The separator of the value is determined by $ ifs.
1 For LineIn $ ( Cat Data. dat) 2 Do 3 Echo " File: $ {Line} " 4 Done 5 6 For Line In ' Cat Data. dat' 7 Do 8 Echo " File: $ {Line} " 9 Done
If there is no space in each line of the input text, line cyclically takes values in the input text based on the Line Break separator.
If the input text contains spaces or tabs, It is not read by line breaks. line can be set to space separators, tabs, or line breaks in the input text.
You can set ifs as a line break to read data row by row.
The default value of IFS is blank (including spaces, tabs, and line breaks ).