Awk advanced applications

Source: Internet
Author: User
Tags first string
  1. If condition judgment

    Syntax 1

    If (expression)

    Action 1

    Else

    Action 2

    Syntax 2

    If (expression) Action 1; else action 2

    # An alarm is triggered when the available capacity of the boot partition is less than 20 mb. Otherwise, OK is displayed.

    DF | grep 'boot' | awk '{if ($4 <20000) print "alart"; else print "OK "}'

  2. While Loop

    Syntax 1

    While (condition)

    Action

    Example:

    X = 1

    While (I <10 ){

    Print $1

    }

    Example:

    Awk 'I = 1 {} begin {While (I <= 10) {++ I; print I} 'test.txt

    Syntax 2

    Do

    Action

    While (condition)

    Example:

    Awk 'in in {do {++ X; print x} while (x <= 10)} 'test.txt

  3. For Loop

    For (variable; condition; Counter)

    Action

    Example:

    Awk 'in in {for (I = 1; I <= 10; I ++) print I} 'test.txt
    Awk 'in in {for (I = 10; I> = 1; I --) print I} 'test.txt

  4. Break and continue

    Break bounce cycle

    Continue ends the current loop

    For (I = 1; I <= 10; I ++ ){

    If (I = 5)

    Continue

    Print I

    }

    For (I = 1; I <= 10; I ++ ){

    If (I = 5)

    Break

    Print I

    }

  5. Function

    5.1 rand () Functions

    Awk 'in in {print rand (); print srand ()} 'test.txt

    5.2 gsub (x, y, z) Functions

    In string Z, use string y to replace all strings that match the regular expression X. The default value of Z is $0.

    Awk-F: 'gsub (/root/, "CCC", $0) {print $0} '/etc/passwd
    CCC: X: 0: 0: CCC:/bin/bash

    5.3 sub (x, y, z) Functions

    In string Z, use string y to replace the first string that matches Regular Expression X. The default value of Z is $0.

    Awk-F: 'sub (/CCC/, "root", $0) {print $0} '/etc/passwd
    CCC: X: 0: 0: CCC:/bin/bash

    5.4 length (z) Function

    Purpose: display the length of each line in the test.txt document:

    Awk '{print length ()}'/usr/local/src/Allen/test.txt

    5.5 Getline () function

    DF-H | awk '{If (NF = 1) {Getline; print $3}; If (NF = 6) Print $4 }'

    DF-H | awk 'in in {print "Disk Free :"}\

    {If (NF = 1) {Getline; print $3}; If (NF = 6) {print $4 }}'


This article from the "quiet fan Yin" blog, please be sure to keep this source http://mastters.blog.51cto.com/6516495/1532880

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.