How to use the awk command in Linux

Source: Internet
Author: User

How to use

awk ' {pattern + action} ' {filenames}

Although the operation can be complex, the syntax is always the same, where pattern represents what AWK looks for in the data, and the action is a series of commands that are executed when the matching content is found. Curly braces ({}) do not need to appear in the program at all times, but they are used to group a series of instructions according to a specific pattern. pattern is the regular expression to be represented, surrounded by slashes.

Example

Awk takes out the specified line, such as Line 20th

cat/etc/hosts | awk ' nr==20 '
Awk takes out rows with 3 columns

cat/etc/hosts | awk ' nf==3 '
Print out the specified column

cat/etc/hosts | awk ' {print $} '
Print out the last column, the length of the column is uneven, but also print the last column

cat/etc/hosts | awk ' {print $NF} '
To remove a random row of a file using a remainder method

Countline= ' Cat Swapfile | Wc-l '
awk "nr==$ ($RANDOM% $countline))" Swapfile

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.