Regular expression tool (part two)
Awk
Common usage of 1.awk
Typically, the command format used by awk is as follows, where single quotation marks and curly braces "{}" are used to set the processing action on the data. Awk can process the target file directly or through the "- f" read script. By default, the delimiter of the field is a space or the tab key. The result of awk execution can be printed and displayed through the function of print. In the process of using the awk command, you can use the logical operator "& &" for "and", "|" for "or", "!" It means "not"; you can also perform simple mathematical operations, such as +, -, *, /,%, ^ for addition, subtraction, multiplication, division, remainder, and multiplier, respectively.
Awk option 'mode or condition {edit instruction}' file 1 file 2 / / filter and output the contents of the file character condition
Awk-f script file 1 file 2 / / call edit instructions from the script, filter and output content
2 built-in variables
FS: specifies the field separator for each line of text, which defaults to spaces or tab stops.
NF: the number of fields in the rows currently being processed.
NR: the line number (ordinal) of the currently processed row.
$0: the entire line content of the currently processed row.
$n: the nth field (nth column) of the currently processed row.
FILENAME: the name of the file being processed.
RS: data records are separated. The default is\ n, that is, one record per behavior.
3 examples of usage
1) output text by line
Awk'{print} 'test.txt / / outputs everything, which is equivalent to cat test.txt
Awk'{print $0} 'test.txt / / outputs everything, which is equivalent to cat test.txt
Awk 'NR==1,NR==3 {print}' test.txt / / output line 1-3
Awk'(NR > = 1) & & (NR