Linux Shell Programming Four Musketeers-awk

Source: Internet
Author: User
Tags arithmetic operators logical operators modifier

Awk
" Syntax "

awk "option" pattern action filenameprogram:pattern+action

If there are multiple statements in the action, you need to use;
Eg:awk ' begin{test= "Hello gawk";p rint test} '
1. Print
Print item1,item2, ...
1) Comma delimiter
2) Each item of the output can be a string or a numeric value: The field, variable, or awk expression of the current record
3) If the item is omitted, it is equivalent to print $;
2. Variables

The reference variable in awk is not required to use $, note the difference between using $ and not using $
2.1 Built-in variables
Fs:input field separator, default to white space characters
Ofs:output field separator, default to white space characters
Rs:input record separator, default line break
Ors:output record separator, line break at output

eg

awk ' begin{fs= ': "; ofs=" \ t "}nr==1{print $1,$2} '/etc/passwd awk-v fs=": "-v ofs=" \ T "' Nr==1{print $1,$2} '/etc/passwd


        nf:number of field, number of fields
             {print nf},{print $NF}
        nr:number of record, lines
   & nbsp;    FNR: Each file counts: number of rows
        filename
             awk ' {print FILENAME} '/etc/fstab
        ARGC : Number of command-line arguments
            awk ' begin{print ARGC} '/etc/fstab
         ARGV: Save the parameters given by the command line
            awk ' Begin{print argv[1]} '/etc/fstab
    2.2 custom variable
         1) Variable names are case-sensitive
            -v var=value
           

Awk-v test= "Hello gawk" ' {print test} '/etc/fstab awk-v test= "Hello gawk" ' Begin{print test} '

2) defined directly in the pattern

awk ' begin{test= ' Hello gawk ';p rint test} '

3) defined directly in action, with different commands, and defined in pattern as distinct

awk '/^title/{i=1;print $} '/etc/grub.conf

3. printf Command
Formatted output: printf format,item1,item2,...
1) format must give the
2) does not wrap, need to display the line feed control \ n
3) Specify a format symbol for each item behind it in format.
format characters:
%c: ASCII code for displaying characters
%d,%i: Displaying decimal integers
%e,%e: Numerical display of scientific counting method
%f: Displayed as a floating-point number
%g,%g: Displaying values in scientific notation or floating-point form
%s: Display string
%u: unsigned integer
Percent: show% itself
eg

awk ' {printf '%s\n ', ' $ '/etc/fstabawk ' {printf ' username:\t%s\n ', ' $ '/etc/fstabawk ' {printf ' filesytem:\t%s\tsize:\t %d\n ", $1,$2} '/etc/fstab

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A2/88/wKioL1mhdtawcJ0qAAAoQqhJZOE970.png "title=" P1.png "alt=" Wkiol1mhdtawcj0qaaaoqqhjzoe970.png "/>

modifier:
Number[.number] The first digital control displays the width: The second number indicates the precision after the decimal point
%3.1f

-: Align Left

+: Display symbols for numbers


eg

awk ' {printf ' filesytem:\t%15s\tsize:\t%d\n ', $1,$2} '/etc/fstab (default is right-justified)

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A2/88/wKioL1mheGHi_sK7AAAoXfWFxGI346.png "title=" P2.png "alt=" Wkiol1mheghi_sk7aaaoxfwfxgi346.png "/>

* Note and the difference

awk ' {printf ' filesytem:%-15s\tsize:\t%d\n ', $1,$2} '/etc/fstab (left-justified result)

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A2/88/wKioL1mheQSANbIpAAAnrj780t0033.png "title=" P3.png "alt=" Wkiol1mheqsanbipaaanrj780t0033.png "/>

4. Operator
Arithmetic operators
X+y,x-y,x*y,x/y,x^y,x%y
-X
+x: Converting a string to a numeric value
String operator: unsigned operator that represents a string connection
Assignment operators:
=,+=,-=,/=,%=,*=
++,--
Comparison operators
>,>=,<,<=,!=,==
Pattern-matching characters:
~: Whether the mode on the left can be matched by the pattern on the right
!~: Can not be matched
logical operators
&&
||
!
Function call:
Functon_name (Argu1,argu2,...) See Bash is a wonderful programming language, this is the formal
Conditional expressions (belonging to an action, 6th below):
Selector?if-true-expression:if-false-expression
eq

Awk-f: ' {$3>=500?usertype= ' Common user ': usertype= "System User";p rintf "%-15s:%s\n", $1,usertype} '/etc/passwd

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/03/D7/wKiom1mhfwPgJ7YpAAAvq_7S05w295.png "title=" P4.png "alt=" Wkiom1mhfwpgj7ypaaavq_7s05w295.png "/>

5, pattern (Specify the address range of execution; address delimitation)
1) Empty: null mode, matching each line:
2)/regular expression/: Handles only rows that can be matched by the pattern here
eg

awk '/^\//{print $ '/etc/fstab

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/03/D8/wKiom1mhgTmh2_L0AAAJ5CvLyds615.png "title=" P5.png "alt=" Wkiom1mhgtmh2_l0aaaj5cvlyds615.png "/>

To the match to the content to reverse

awk '!/^\//{print $ '/etc/fstab

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/03/D8/wKiom1mhgleTfvqHAAAMavhcBCo649.png "title=" P6.png "alt=" Wkiom1mhgletfvqhaaamavhcbco649.png "/>

3) Relational expression: relationship expression: The result is "true" with "false": The result is true before being processed
True: The result is a non-0 value, non-empty string

Awk-f: ' $3>=500{print $1,$3} '/etc/passwd

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/03/D8/wKiom1mhhkWw4ZnMAAAtsSvFU5g393.png "title=" P7.png "alt=" Wkiom1mhhkww4znmaaatssvfu5g393.png "/>

The last column (field) equals/bin/bash

Awk-f: ' $NF = = '/bin/bash ' {print $, $NF} '/etc/passwd

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/03/D8/wKiom1mhhrDSumMZAAAY5Z6csc4455.png "title=" P8.png "alt=" Wkiom1mhhrdsummzaaay5z6csc4455.png "/>

Using the pattern matching in front of you ~

Awk-f: ' $NF ~/bash$/{print $, $NF} '/etc/passwd

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/03/D8/wKiom1mhhrDSumMZAAAY5Z6csc4455.png "title=" P8.png "alt=" Wkiom1mhhrdsummzaaay5z6csc4455.png "/>

4) Line range: Row ranges
Startline,endline

Awk-f: ' Nr>=2&&nr<=10{print $ '/etc/passwd

Then insert the next syntax to strengthen the memory: awk [option] pattern action

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/03/D8/wKiom1mhjUOB21IXAAAUTQhrmxg903.png "title=" P9.png "alt=" Wkiom1mhjuob21ixaaautqhrmxg903.png "/>

5) Begin/end mode
begin{}: Executes only once before beginning processing of each line of text in a file
end{}: Only once after text processing is complete
eg

Awk-f: ' Begin{print ' \tusername\tuid\n------------------------------"}nr>=1&&nr<=5{print $1,$3} '/ etc/passwd

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A2/89/wKioL1mhj3ngp6MqAAAQWs0DXg8569.png "title=" P1.png "alt=" Wkiol1mhj3ngp6mqaaaqws0dxg8569.png "/>

Note that the difference between this example and the previous example is that the begin content is placed in the main program.

Awk-f: ' Nr>=1&&nr<=5{print ' \tusername\tuid\n------------------------------";p rint $1,$3} '/etc/ passwd

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/03/D9/wKiom1mhkUrw_KS3AAAlecmfLFw321.png "title=" P3.png "alt=" Wkiom1mhkurw_ks3aaalecmflfw321.png "/>

Awk-f: ' Begin{print "\tusername\tuid\n------------------------------"}nr>=1&&nr<=5{print $1,$3}end{ Print "=====================end=========================="} '/etc/passwd

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/03/D9/wKiom1mhkC6RwE48AAAXtlP2VkU840.png "title=" P2.png "alt=" Wkiom1mhkc6rwe48aaaxtlp2vku840.png "/>


6. Commonly used action
1) Expressions Expression
2) Control Statements Controls statement (The following 7th article is detailed)
If
While
3) Compound Statements Combination statement
4) Input Statements Enter statement
5) Outpput Statements output statement
7. Control Statements
if (condition) {statements}
if (condition) {statements} else {statements}
while (condition) {statements}
Do {statements} while (condition)
for (EXPR1;EXPR2;EXPR3) {statements}
Break
Continue
Delete Array[index]
Delete array
Exit
{statements}
7.1 If-else
Syntax: if (condition) {statements} else {statements}

Single branch:

Awk-f: ' {if ($3>=500) {print $1,$3}} '/etc/passwd

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/03/D9/wKiom1mhl5KTC0b7AAArLp9eHbE105.png "title=" P1.png "alt=" Wkiom1mhl5ktc0b7aaarlp9ehbe105.png "/>

Dual Branch

Awk-f: ' Nr>=1&&nr<=5{if ($3>=500) {printf "Common user:%s\n", $ "else {printf" System user:%s\n ", $ {}} ' /etc/passwd

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A2/89/wKioL1mhmXWiQr5NAAAOm4YoFZg999.png "title=" P2.png "alt=" Wkiol1mhmxwiqr5naaaom4yofzg999.png "/>

Usage scenario: Make a conditional judgment on the entire row or field that awk obtains
The number of fields (fields) in the row is greater than 5, and the output line

awk ' {if (nf>5) {print $}} '/etc/fstab

Exercise: Output a partition with a usage greater than 20%

df-h | awk-f% ' {print $} ' | awk ' Nr>=2{if ($NF >70) {print $}} '

7.2 While Loop
Syntax: while (condition) {statements}
Conditions are true, enter the loop, condition false, exit loop
Usage scenario: used when multiple fields within a row are processed one after the other, using each element in the array
Exercise: Work on a line and count the length of each field

awk '/^title.* 6. */{i=1;while (I<=NF) {print $i, length ($i); i++}} '/etc/grub.conf

Add modifier

awk ' begin{print '---------total-----------"}/^title.* 6. */{i=1;while (I<=NF) {printf"%-25s\t%i\n ", $i, Length ($i) ; I++}}end{print "===========end==========="} '/etc/grub.conf

Deepening difficulty: Adding requirements to existing test instructions: output with fields greater than or equal to 5
Problem solving: nesting if in while

awk ' begin{print '---------total-----------"}/^title.* 6. */{i=1;while (I<=NF) {if (length ($i) >=5) {printf"%-25s \t%i\n ", $i, Length ($i)};i++}}end{print" ===========end=========== "} '/etc/grub.conf

Subsequent other control statements do not list, awk so broad and profound, if not all its functions, do not need to master, otherwise it is unfavorable to the memory, the above can be mastered is enough to use the usual work.


This article is from the "zhaoyfcomeon-Growth Road" blog, make sure to keep this source http://zhaoyfcomeon.blog.51cto.com/8429349/1959685

Linux Shell Programming Four Musketeers-awk

Related Article

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.