Linux shell sed awk command (2)-awk

Source: Internet
Author: User
Tags rand

Linux shell sed awk command (2)-awk


awk syntax Format:

awk [Options]-F program-file [--] file ...

Options:

-F FS,--field-separator FS

Create a delimiter with FS as the input line (the default delimiter is a space or tab)


-V Var=val,--assign var=val

Before executing the procedure, set a var value of Val


-F program-file,--file Program-file

Read the awk instruction from the script file to replace the input processing script in the command arguments


-W compat,-w copyright,--compat,--traditional

Running the awk,gun extension option in compatibility mode is ignored


-W Dump-variables[=file],--dump-variables[=file]

Print global variables (variables, types, values) to a file, and automatically output to a file named Dump-variables if no file name is provided


Awk-w dump-variables=out.txt ' x=1 {print x} ' Test.txt



eg

1. Match blank lines with regular expression/^$/

awk '/^$/{print ' Blank line '} ' eth0

2. Print the line containing the hostname, without specifying an action instruction. Default action is Print

awk '/hostname/'/etc/sysconfig/network

3. Specify the script-F

(1) The contents of awk.sh are as follows:

/^$/{print "Blank line"}

(2) Awk-f awk.sh eth0

Blank Line

Blank Line

Blank Line

Blank Line

Blank Line


Records and Fields

echo Hello World ABC | awk ' {print $1,$2,$3} '

Hello World ABC


echo Hello World ABC | awk ' {print $} '

Hello World ABC


echo Hello World ABC | awk ' {print NF} '

3


echo Hello World ABC | awk ' {print $NF} '

Abc


Field delimiter

Awk-f: ' {print '} '/etc/passwd

awk ' BEGIN {fs= ': '} {print '} ' passwd


Specify multiple separators

echo ' Hello the:world,! ' | awk ' BEGIN {fs= ' [:,] '} {print $1,$2,$3,$4} '


Built-in variables

ARGC command-line arguments

FILENAME Current input document name

FNR Current input Document

Current record number of the NR input stream

NF Current record Field number

FS Character Separator

OFS output field delimiter, default space

ORS output record delimiter, default to line feed \ n

RS input record delimiter, default to line feed \ n


Expressions and operators

Arithmetic +-*/

% fetching ^ Power operation

Self-added self-reduction

+ +--


~ Match! ~ No match



echo "Test" | awk ' x=2 {print x+3} '

Awk-f: ' $1~/root/{print $} '/etc/passwd//print root ID number

Awk-f: ' $3>500 {print '} '/etc/passwd//list user name with ID number greater than 500


 

awk Syntax rules


if (an expression)

Action

Else

Action


An if (expression) action; Else action


Eg:

Interpretation SDA4 capacity is less than 20MB alarm, otherwise display OK

df-h | grep "SDA4" | awk ' {if ($4<20000) print "Alart"; else print "OK"} '

while (condition)

Action


Syntax format:

X=1

while (i<9) {

Print $i

}


eg

awk ' I=1 {} BEGIN {while (i<=10) {++i; print i}} ' test


Do

Action

while (condition)


eg

awk ' BEGIN {do {++x;print x} while (x<=10)} ' test


for (variable; condition; counter)

Action


eg

awk ' BEGIN {for (i=1;i<=10;i++) print i} '


Function

1.rand ()

awk ' BEGIN {print rand (); Srand ();p rint srand ()} '


2.gsub (x, Y, z), sub (x, Y, z)


Awk-f: ' Gsub (/root/, "Rockycai", $ $) {print $} '/etc/passwd//Global Replace

Awk-f: ' Sub (/root/, "Rockycai", $ $) {print $} '/etc/passwd//local replacement


3.length (z)

awk ' {print length ()} '/etc/passwd//display passwd per line length


4.getline

df-h | awk ' BEGIN {print ' Disk free: '}{if (nf==1) {getline;print $3};if (nf==6) print $4} '



Linux shell sed awk command (2)-awk

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.