Shell Programming Awk Basics Introduction

Source: Internet
Author: User

awk Introduction

Report Generator, formatted text output

The processing mechanism is similar to the SED command, which comes with loop processing, reads a line and then automatically reads the next line to process

The identity of the SED command newline is fixed and can only be a carriage return line break. The newline characters inside awk are customizable.

Awk automatically cuts the rows that are read into several fields by the specified cut symbol the default cut symbol is a blank symbol (including contiguous blank symbols, TAB key, carriage return line break)

Basic syntax

awk [Options] ' program ' Var=value file

awk [Options]-F programfile var=value file

awk [options] ' begin{action;.} pattern{Action;   END {action; ...} ' File

Typically, the BEGIN statement block consists of three parts of the universal statement block End Statement block that can use pattern matching

First step: Execute Bgein statement blocks are often used to print headers

Second step: Read a line from a file or standard input, and then execute the pattern statement it will scan the file row by line until all the files have been read

Step three: After you finish processing the file or the last line of the input stream, the last statement block is often used for data summarization

The pattern statement does not provide a default execution {print} is to print every read to the row

Option options

-F indicates the field delimiter used when entering

-V var=value Custom variable

awk built-in variables

FS means that the row is cut into columns according to this variable.
The OFS means that the column is assembled and combined by this character output
RS indicates that this variable is used as the delimiter for the row
The ORS represents the result of connecting each line of output with this symbol
NF indicates the size of the number of fields
NR denotes line number
FNR multiple files when each file line number
filename indicates the current file name

FS: Enter field delimiter, default to white space character

Awk-v fs= ': ' {print $1,fs,$3} '/etc/passwd

Awk–f: ' {print $1,$3,$7} '/etc/passwd

OFS: Output field delimiter, default to white space character

Awk-v fs= ': '-v ofs= ': ' {print $1,$3,$7} '/etc/passwd

RS: Enter the record delimiter, specify the line break when entering the default is a newline character

Awk-v rs= ' {print} '/etc/passwd

ORS: Output record delimiter, output with specified symbol instead of line break default is line break

Awk-v rs= "-v ors= ' # # # ' {print} '/etc/passwd

NF: Number of fields

Awk-f: ' {print NF} '/etc/fstab

Awk-f: ' {print NF, '----' $NF '----' $ (NF-1)} '/etc/passwd

Reference built-in variables without $

Awk-f: ' {print $ (NF-1)} '/etc/passwd

NR: Record number

awk ' {print NR} '/etc/fstab; awk END ' {print NR} '/etc/fstab

FNR: Each file counts its own record number separately

awk ' {print FNR} '/etc/fstab/etc/inittab

FileName: Current file name

awk ' {print FILENAME} '/etc/fstab

ARGC: Number of command line arguments

awk ' {print ARGC} '/etc/fstab/etc/inittab

awk ' BEGIN {print ARGC} '/etc/fstab/etc/inittab

ARGV: An array that holds the arguments given by the command line

Awk's "BEGIN {print argv[0]} '/etc/fstab/etc/inittab argv[0] represents the argv[1] representing/etc/fstab Argv[2] on behalf of/etc/initta B

awk ' BEGIN {print argv[1]} '/etc/fstab/etc/inittab

awk Custom Variables

Custom variables (character case sensitivity)

(1)-V Var=value

(2) directly defined in program

(3) It is best to define the assignment before you use it.

printf formatted output

1. Output information is not wrapped by default, you need to display the specified line break control \ n

Format characters:

1.%c Displays the ASCII code of the character

2.%d,%i Display decimal digits

3.%s Display string

4. Percent display% of itself

5.%f Display floating point number

Awk-f: ' {printf '%s:%s\n ', $1,$3} '/etc/passwd

Awk-f: ' begin{print ' username uid '}{printf '%s%s\n ', $1,$3} '/etc/passwd

awk instances
1 Read program from File2 CatAwkscript3{Print script,$1,$2} 4[Email protected] ~]#awk-F:-F Awkscript script="AWKKK"/etc/passwd5 awkkk root x6 Awkkk bin x7 awkkk daemon x8[Email protected] ~]#awk-F:'{printf "Username:%s\n", $ {}'/etc/passwd9 Username:rootTen Username:bin One Username:daemon A Output JSON dictionary -[Email protected] ~]#awk-F:'{printf "username:%s,uid:%d\n", $1,$3}'/etc/passwd -Username:root,uid:0 theUsername:bin,uid:1 -[Email protected] ~]#awk-F:'$ ~/root/{print $}'/etc/passwd - Root - operator + Rooter -[Email protected] ~]#awk-F:'$ ~/^root/{print $}'/etc/passwd + Root A Rooter at awk '$ !~/root/'/etc/passwd -BIN:X:1:1: bin:/bin:/sbin/Nologin -DAEMON:X:2:2:d aemon:/sbin:/sbin/Nologin -[Email protected] ~]#awk-F:'$3==0'/etc/passwd -ROOT:X:0:0: Root:/root:/bin/bash
View Code
1[Email protected] ~]#awk '{print 2^10}'2 e3 1024x7684[Email protected] ~]#awk-F:'{print $ ":" $ $}'/etc/passwd5Root06Bin17Daemon28[Email protected] ~]#DF|awk '{print $1,$3}'9 Filesystem usedTen/dev/sda24347496 OneDevtmpfs0 ATmpfs0 -[Email protected] ~]#awk-V fs=:'{print $1,$3}'/etc/passwd -Root0 the[Email protected] ~]#awk-V fs=:'{print $1fs$3}'/etc/passwd -Root0 - shell variables passed to awk -[Email protected] ~]# fs=":";awk-V fs= $fs'{print $1fs$3}'/etc/passwd +Root0 -Bin1 +  A[Email protected] ~]#awk-V ors="****" '{print $}'A2 at 1****3o****2o****4You have new mailinch/var/spool/mail/Root -[Email protected] ~]#awk '{print $}'A2 - 1 - 3 - o - 2 in o - 4
View Code
1[Email protected] ~]#DF|grep '^/DEV/SD'|awk '{print $1,$5}'2/dev/sda29%3/dev/sda31%4/dev/sda1 -%5[Email protected] ~]#DF|grep '^/DEV/SD'|awk-V fs=%'{print $}'6/dev/sda252403200 4347456  48055744   97/dev/sda331441920   83004  31358916   18/dev/sda11038336  198256    840080   -9[Email protected] ~]#DF|grep '^/DEV/SD'|awk-V fs=%'{print $}'|awk '{print $1,$5}'Ten/dev/sda29 One/dev/sda31 A/dev/sda1 - -[Email protected] ~]#awk-F:'{print NF, $NF, $ (NF-1)}'/etc/passwd - 7/bin/bash/Root the 7/sbin/nologin/bin - 7/sbin/nologin/Sbin - 7/sbin/nologin/var/ADM - 7/sbin/nologin/var/spool/lpd
View Code

Shell Programming Awk Basics Introduction

Related Article

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.