Linux Command awk

Source: Internet
Author: User

Linux Command awk

Awk is a powerful text analysis tool that reads files row by row. It uses spaces as the default delimiter to segment each line and then analyzes and processes the cut parts.

Awk command format:

Awk [-F |-f |-v] 'in in {} // {command1; command2;...; commandN} END {} 'file

 

[-F |-f |-v]: Specifies the separator for a large parameter.-F calls the script and-v defines the variable.

'': Reference code block

BEGIN: Initialize the code block. Before processing each line, initialize the Code. (In the script, it mainly references global variables and sets the FS separator)

//: Matching style, which can be a string or regular expression

{}: Command code block, which contains one or more commands

;: Separate multiple commands with semicolons

END: the code block that is executed after processing each line. It is mainly used for final calculation or output summary information.

Arithmetic operations supported by awk:

+-*/^ % + + -- + =-= * =/= % =

Awk built-in variables:

$0 indicates the entire row.

$ N indicates the nth field in each line. n cannot be 0.

NF field quantity variable

Indicates the number of records in each row of NR. Multi-file processing increases the number of records.

The record number of each FNR line, which does not increase progressively when processing multiple files. Each file starts from 1.

Define the delimiter when FS BEGIN

The record delimiter input by RS. The default value is a line break.

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

The delimiter of the ORS output record. The default Delimiter is a line break.

Example file emp_names
$ cat emp_names 46013     DURHAM JEFF      MOBILE     AL46015     STEEN BILL       MOBILE     AL46017     FELDMAN EVAN     MOBILE     AL46018     SWIM STEVE       UNKNOWN    AL46019     BOGUE ROBERT     PHOENIX    AZ46021     JUNE MICAH       PHOENIX    AZ46022     KANE SHERYL      UNKNOWN    AR46024     WOOD WILLIAM     MUNCIE     IN46026     FERGUS SARAH     MUNCIE     IN46027     BUCK SARAH       MUNCIE     IN46029     TUTTLE BOB       MUNCIE     IN
$ Awk '{print $0}' emp_names # print all fields in the emp_names file 46013 durham jeff mobile AL46015 steen bill mobile AL46017 feldman evan mobile AL46018 swim steve unknown AL46019 bogue robert phoenix Jun JUNE MICAH PHOENIX AZ46022 kane sheryl unknown AR46024 wood william muncie IN46026 fergus sarah muncie IN46027 buck sarah muncie IN46029 tuttle bob muncie in $ awk '{print $1, $2, $3, $4, $5} 'emp_names # print multiple fields in the emp_names file: 46013 durham jeff mobile AL46015 steen bill mobile AL46017 feldman evan mobile AL46018 swim steve unknown uncle Boger robert phoenix Jun june micah phoenix parse KANE SHERYL UN AR46024 wood william muncie IN46026 fergus sarah muncie IN46027 buck sarah muncie IN46029 TUTTLE BOB MUNCIE IN
$ Awk '/AL | AZ/{print $3, $2, $5} 'emp_names # print multiple fields matching the AL and AZ styles in the emp_names file jeff durham albill steen alevan feldman alsteve swim alrobert bogue azmicah june az $ awk '$5 ~ /AR/'emp_names # match the style among the 5th fields. If no print field is specified, all fields are printed. 46022 kane sheryl unknown ar $ awk '$5 !~ /AR/'emp_names # match the style in the 5th fields and print out all rows that do not match (print fields are not specified, print all fields) 46013 durham jeff mobile Short steen bill mobile Short feldman evan mobile AL46018 swim steve unknown AL46019 bogue robert phoenix Jun june micah phoenix parse wood william muncie short fergus sarah muncie short buck sarah muncie short TUTTLE BOB MUNCIE IN $ awk '$5 ~ /AR/{print $1; print $2; print $3} 'emp_names # print 46022 KANESHERYL
$ Awk '{print $0 | "sort>/tmp/filez"}' emp_names # sort fields in the file emp_names, redirect to the/tmp/filez File $ cat/tmp/filez 46011 tuttle bob muncie IN46012 steen bill mobile guest durham jeff mobile AL46017 feldman evan mobile AL46018 bogue robert phoenix AZ46019 swim steve unknown AL46021 JUNE micah phoenix AZ46022 kane sheryl unknown AR46027 buck sarah muncie IN46030 fergus sarah muncie IN46045 WOOD WILLIAM MUNCIE IN
 
$ Awk-F ":" '{print $1}'/etc/passwd #/etc/passwd is a system-provided file. Each line is separated, therefore, you need to specify the separator or use awk '{FS = ":"} {print $1}'/etc/passwd. The result is the same rootdaemonbinsyssyncgamesmanlpmailnews...
$ Cat awk. list BEGIN {FS = ":"} {print $1} $ awk-f awk. list/etc/passwd # Use-f to call the awk script rootdaemonbinsyssyncgamesmanlp
$ Awk '{FS = ":"} {OFS = "-"} {print $1, $3}'/etc/passwd # specify the output field separator root: x: 0: 0: root:/bin/bash-daemon-1bin-2sys-3sync-4games-5man-6lp-7 $ awk-F ":" '{print "USER_NAME:" $1, "\ nNUMBER: "$3} '/etc/passwd # output USER_NAME: root NUMBER: 0USER_NAME: daemon NUMBER: 1USER_NAME: bin NUMBER: 2USER_NAME: sys NUMBER: 3USER_NAME: sync NUMBER: 4USER_NAME: games NUMBER: 5

 

Sample file content $ cat inventory Contents

 

$ Awk 'in in {print "ITEM \ tQUANTITY \ tPRICE \ tTOTAL"} {x = x + ($2*$3 )} {print $1 "\ t" $2 "\ t" $3 "\ t" $2 * $3} END {print "Total Value of Inventory: "x} 'ventoryitemquantitypricetotal # BEGIN keyword occurrence Value of Inventory: 2241.66 # END keyword x final Value

Awk is a programming language. It also supports if statements and loop statements. Its usage is similar to that of C language programming.

Zookeeper

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.