Awk command operation details

Source: Internet
Author: User
  1. Record and Field

    Awk reads a record from the file at a time and stores the record in the field variable $0. Records are separated into fields. The function is stored in $1, $2,... $ NF. (space or indicator Delimiter is used by default ). The built-in parameter NF is the number of recorded fields.

    Outputs 1st, 2nd, 3rd, and 4th fields of the demo.txt file.

    Awk '{print $1, $2, $3, $4}' demo.txt

    Parse outputs the entire line of the demo.txt File

    Awk '{print $0}' demo.txt

    The number of fields in this row in demo.txt output by workers.

    Awk '{print NF}' demo.txt

    The number of fields in this row in demo.txt output by workers.

    Awk '{print $ NF}' demo.txt

  2. Field Separator

    By default, awk reads data using spaces or indicator characters as separators. However, you can use the-f or FS (field separator) variable to change more characters.

    Awk-F: '{print $1}'/etc/passwd
    Awk 'in in {FS = ":"} {print $1} '/etc/passwd

    Echo 'Hello The: World ,! '| Awk' begin {FS = "[:,]"} {print $1, $2, $3, $4 }'

  3. Built-in variable description

    Number of argc command line parameters

    Filename name of the current input file

    FNR indicates the number of the current record of the current input document, especially when multiple input documents exist.

    Current input record number of the NR input stream

    Number of fields recorded by NF

    FS Field Separator

    Specifies the delimiter of the output field of OFS. The default Delimiter is space.

    ORS output record delimiter. The default value is line break \ n.

    RS input record delimiter. The default value is line break \ n.

    Cat test1.txt

    This is a test file.
    Welcome to Jacob's class.

    Cat test2.txt

    Hello the world.
    Wow, I'm overwhelmed.
    Ask for more.

    # Output the current line number of the current document

    Awk '{print FNR}' test1.txt test2.txt

    # Use the two documents as a whole input stream and enter the current row number through NR

    Awk '{print Nr}' test1.txt test2.txt

    # Output the current number of fields for each row

    Awk '{print NF}' test1.txt

    # Built-in variable FS field delimiter

    Awk 'in in {FS = "[:]"} {print $1} '/etc/passwd

    # Output field case delimiter

    Awk 'in in {OFS = "-"} {print $1, $2, $3} 'test1.txt

    Cat test.txt

    Name = lianjie
    Age = 26
    Tel = 18219995524

    Name = ccc
    Age = 20
    Tel = 13899246656

    Name = Alex
    Age = 25
    Tel = 19812345623

    Awk 'in in {FS = "\ n"; RS = ""} {print $1, $2, $3, $4} 'test.txt
    Name = lianjie age = 26 Tel = 18219995524
    Name = CCC age = 20 Tel = 13899246656
    Name = Alex age = 25 Tel = 19812345623

    Awk 'in in {FS = "\ n"; RS = ""} {print $1} 'test.txt

  4. Expressions and operators

    Operator

    + Add

    -Subtraction

    * Multiplication

    /Division

    % Remainder

    ^ Power Operation

    ++ Auto-increment 1

    -- Auto-minus 1

    + = (X + = 9 is equivalent to X = x + 9)

    -= (X-= 9 is equivalent to X = X-9)

    * = (X * = 9 is equivalent to X = x * 9)

    /= (X/= 9 is equivalent to X = x/9)

    > Greater

    <Less

    > = Greater than or equal

    <= Less than or equal

    ! = Not equal

    ~ Match

    !~ Not worthy

    &

    | Or

    Echo 'test' | awk 'X = 2 {print x + 3 }'

    Echo 'test' | awk 'X = 2, y = 3 {print x * 3, y * 3 }'

    Awk '/^ $/{print x + = 1}' test.txt # Count all blank rows

    Awk '/^ $/{x + = 1} end {print x}' test.txt # print the total number of blank rows

    Awk-F: '$1 ~ /Root/{print $7} '/etc/passwd

    Awk-F: '$3> 500 {print $1}'/etc/passwd

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

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.