Awk command usage

Source: Internet
Author: User

Format: awk '{Pattern + action}' {filenames}

Common parameters:

-F specifies that the Delimiter is a space by default.

Start with begin printing

End print end

Built-in Variables

Number of argc command line parameters

Argv command line parameter arrangement

Environ supports the use of system environment variables in the queue

Filename awk browsed file name

Number of FNR browsing file records

FS sets the input domain separator, which is equivalent to the-F Option

Number of NF browsing records

Number of records read by NR

OFS output domain Separator

ORS output record Separator

RS control record delimiter

Example:

-F usage

[[Email protected] ~] # Cat/etc/passwd | awk-F: '{print $1 "\ t" $7}' | head-5

Root/bin/bash

Bin/sbin/nologin

Daemon/sbin/nologin

ADM/sbin/nologin

LP/sbin/nologin

Use of begin and end

[[Email protected] ~] # Cat/etc/passwd | awk-F: 'In in {print "User Shell"} {print $1 "\ t" $7} end {print "test/bin/bash "}'

User Shell

Root/bin/bash

Bin/sbin/nologin

Daemon/sbin/nologin

ADM/sbin/nologin

....

Test/bin/bash

Matching search

[[Email protected] ~] # Cat/etc/passwd | awk-F: '{if ($1 ~ /Root/) Print $1 "\ t" $7 }'

Root/bin/bash

Exact match

[[Email protected] ~] # Cat/etc/passwd | awk-F: '$1 = "root" {print $0 }'

Root: X: 0: 0: Root:/root:/bin/bash

Mismatch

[[Email protected] ~] # Cat/etc/passwd | awk-F: '{if ($1 !~ /Root/) Print $1 "\ t" $7 }'

Exact Mismatch

[[Email protected] ~] # Cat/etc/passwd | awk-F: '$1! = "Root" {print $0 }'

Less

Awk '{if ($6 <$7) Print $0 "$6 less than $7 "}'

Greater

Awk '{if ($6 >$ 7) Print $0 "$6 greater than $7 "}'

Any character

It records the fourth character of the first domain as a, and uses a period .. Expression/^... A/indicates any of the first three characters of the line, the fourth is A, and the sharp angle symbol indicates the first line.

Awk '$1 ~ /^... /'

Or link match

Awk '$0 ~ /(ABC | 123 )/'

And or usage

& Indicates that both sides are true.

| It means that the two sides match. If there is a truth, it is true.

Awk '{if ($1 = "ABC" & $4 = "123") Print $0 }'

Awk '{if ($1 = "ABC" | $4 = "123") Print $0 }'

Statistics/etc/passwd: file name, row number of each row, column number of each row, corresponding to the complete row content:

Filename, NF, nr is equivalent to CAT/etc/passwd | WC-l

[[Email protected] ~] # Awk-F': ''{print" filename: "FILENAME", linenumber: "Nr", columns: "NF", linecontent: "$0} '/etc/passwd

Filename:/etc/passwd, linenumber: 37, columns: 7, linecontent: mysql: X: 27: 27: MySQL Server:/var/lib/MySQL:/bin/bash

Count the number of bytes occupied by a folder

Ll | awk 'in in {size = 0 ;}{ size = size + $5 ;}end {print "total size is", size/1024/1024, "M "}'

Total size is 201.351 m

Statistics and

Awk '{sum + = $2} end {print sum} 'aa


This article is from the "Linux" blog, please be sure to keep this source http://520and519.blog.51cto.com/2254416/1557712

Awk command usage

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.