AWK 学习手札之一: an AWK tutorial

these article base on the AWK programming language by alfred v.aho brian W.Kernighan and Peter J.Weinberger.

1.1 geting start:

generate a file name emp.data, which contain the name of employee, pay rate in dollar perl hour and worked hour, one employee record per line

Beth    4.00    0
Dan    3.75    0
Kathy    4.00    10
Mark    5.00    20
Mary    5.50    22
Susie    4.25    18

now you want to print the name and pay for everyone who worked more than zero hour, type the comman line:

1  awk '$3>0{print $1,$2*$3}' emp.data

your will get this output:

Kathy 40
Mark 100
Mary 121
Susie 76.5

the part inside the quotes is the complete awk program. it consists of a single pattern-action statement.

the pattern: $3>0, matchs every input line in which the third column, or field, is greater than zero, and the action

{print $1,$2*$3}

prints the first field and theproduct of the second and third fields of each matched line.

if you want to print the name of those employees who did not work, type this command line:

1  awk '$3>0{print $1,$2*$3}' emp.data

you will get:

Beth

Dan

1.2 the structure of an awk program

each awk program in this chapter is a sequence of one or more pattern-action statements:

pattern {action}

the basic operation of awk is to scan a sequence of input lines one after another, searching for lines that are matched by any of the patterns in the program.

1.3 running awk programming

awk 'program' input files

awk 'program'

原文地址:https://www.cnblogs.com/hanleilei/p/2291203.html