9.6 awk (UP)
Awk is a programming language that is used to process text and data under Linux/unix. The data can come from standard input (stdin), one or more files, or the output of other commands. It supports advanced functions such as user-defined functions and dynamic regular expressions, and is a powerful programming tool under Linux/unix. It is used in the command line, but more is used as a script. Awk has many built-in features, such as arrays, functions, and so on, which are the same as the C language, and flexibility is the biggest advantage of awk.
HEAD-N2 Test.txt | Awk-f ': ' {print $ '----> intercept the 1th field in the first, two lines of a document
HEAD-N2 Test.txt | Awk-f ': ' {print $} '----> intercept the first, two lines in the document
HEAD-N2 Test.txt | Awk-f ': ' {print $ "#" $ $ "#" $ $ "#" $4} '----> intercepts the first, two lines of the 1,2,3,4 field in a document
awk '/oo/' test.txt----> intercept documents containing OO lines
Awk-f ': '/root/{print $1,$3}/user/{print $3,$4} ' test.txt----> intercept the document containing the root row 1, 3 fields and 3, 4 fields containing the user row
Awk-f ': ' $3== ' 0 ' test.txt----> Intercept the 3rd field in the document equals 0 lines
Awk-f ': ' $3>= ' test.txt----> Intercept the 3rd field in the document is greater than the line that is equal to the string (ASCLL value)
Awk-f ': ' $3>=500 ' test.txt----> Intercept the 3rd field in the document is greater than or equal to 500 rows
Awk-f ': ' $7!= '/sbin/nologin ' test.txt----> Intercept the 7th field in the document does not equal the row of/sbin/nologin
Note:
The function of the-f option is to specify a delimiter, or a space or tab as a separator if no-f is specified. Print is the printed action used to print out a field. $ $ for the first field, and $ for the second field, and so on, there is a special that is $ A, which represents the entire row.
9.7 awk (bottom)
Awk-f ': ' $3<$4 ' test.txt----> Intercept a row in the 3rd field of a document that is less than the value of 4 fields
Awk-f ': ' $3> "5" && $3< "7" ' test.txt----> Intercept the 3rd field in the document greater than 5 less than 7 lines
Awk-f ': ' $3>1000 | | $7== "/bin/bash" ' test.txt----> Intercept the 3rd field in the document is greater than 1000 and the 7th field is not equal to the/bin/bash row
- Truncate the first 5 lines of the document in the 1,3,4 field and use # as the delimiter output:
Head-n5 Test.txt | Awk-f ': ' {ofs= ' # '}{print $1,$3,$4} '
- Truncate the 1,2,3,4 field of the 3rd field in the document that is greater than 1000 and use # as the delimiter output:
Awk-f ': ' {ofs= ' # '}{if ($3>1000) {print $1,$2,$3,$4}} ' Test.txt
Head-n3 Test.txt | Awk-f ': ' {pirnt NF} '----> intercept the first 3 lines of the document and then print out the number of segments for each line
Head-n3 Test.txt | Awk-f ': ' {pirnt NR} '----> intercepts the first 3 lines of a document and prints the line number of each line
awk ' nr>40 ' test.txt----> Intercept all lines starting at line 40th
Head-n3 Test.txt | Awk-f ': ' $1= ' root '----> truncate the first 3 lines of the document and change the 1th field of each line to root
Awk-f ': ' {(tot=tot+$3)}; END {print tot} ' test.txt----> sums and prints the 3rd field in each line of the document
Awk-f ': ' {if ($1== "root") {print $}} ' test.txt----> if the 1th field of a row equals root, print the row
Awk-f ': ' nr<=20 && ~/roo/' test.txt----> Print Document the 1th field in the first 20 rows contains all rows of roo
Note:
The variables commonly used by AWK are:
NF: Number of segments, separated by delimiter, how many segments are there
NR: Number of rows
OFS: Specify delimiter when printing out
END: Indicates that all rows have been executed
2018-1-17 Linux Learning Notes (awk) [Important]