Shell's awk

Source: Internet
Author: User

Brief introduction

Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.


How to use:

awk [option] ' Pattern1{action1}pattern2{action2}' {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 a match is found.

Awk built-in three variables

NF: Indicates that all current fields $nf represents the last field

FS: Default to Space-character separators

What line Nr:awk read to


awk Basic Usage

1, regexp: Regular expression, the format is/regular expreesion/

Awk-f: ' $3>=500{printf $ "\ n" \ n "} '/etc/passwd

2. Expression: expressions, which meet the criteria only if they are not 0 or non-null characters, such as ~ to indicate a match (similar to grep lookup line match)

Awk-f: ' $7!~ ' bash$ ' {printf $7 ' \ n '} '/etc/passwd

3, Ranges: Specify the matching range, the format is PAT1,PAT2

Awk-f: ' $ = = 0,$7~ ' nologin$ ' {printf $ ' \ t ' $7 ' \ n '} '/etc/passwd

4, Begin/end: Special mode,

Awk-f: ' begin{printf "Username shell\n"}$3 = = 0,$7~ "nologin$" {printf $ \ t "$7" \ n "} '/etc/passwd

5. Empty mode: Indicates no matching option


This article is from "think one or two" blog, please be sure to keep this source http://250919938.blog.51cto.com/962010/1918868

Shell's awk

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.