Awk command usage, awk

Source: Internet
Author: User

Awk command usage, awk

Awk Programming Language/Data Processing Engine

Creator: Aho weinberger kernighan

Check input based on pattern matching (read row-by-row output)

Print the expected matching result to the screen


Syntax format:

Awk 'mode {operation}' file 1 file 2 .....


Common built-in Variables

The ordinal number of the row being processed by NR (row number)

FS fields are separated. The default value is space or Tab space.

$ N the nth field of the current row

$0 all text content of the current row


Output text by line number

Awk 'nr = 1, NR = 3 {print} 'file.txt

Awk '(NR = 1) | (NR = 3) {print}' file.txt


Use comparison operations

Awk '(NR % 2 = 1) {print}' file.txt outputs all odd rows

Awk '(NR % 2 = 0) {print}' file.txt outputs all even rows


Use Regular Expressions

Awk '/2/{print}' file.txt

Awk '/base $/{print}' file.txt


Specify the separator and specify the output Fields

Awk 'nr = 2, NR = 3 {print $1, $3} 'file.txt outputs the 1, 3 Field of Row 2-3

Awk-F. '$5 = "yes" {print $0}' file.txt output the fifth field separated by. Contains the rows of yes

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.