2018-1-17 Linux Learning Notes (awk) [Important]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.