Title: Alfred V, Aho, Peter J. Weinberger, ??? Brian W. Kernighan ???????? awk ???? 1977 ???????????????????????????????????????????? ????????????????????????????????????? UNIX ??? C ??????????????? ??????????????????????? C ????? awk
1(No Transcript)
2A Brief history
Alfred V, Aho, Peter J. Weinberger, ??? Brian W.
Kernighan ???????? awk ???? 1977
????????????????????????????????????????????
????????????????????????????????????? UNIX ??? C
??????????????? ??????????????????????? C ?????
awk
3Introduction to Awk
- awk ?????????????????????????????????????????????
??????????????????????????????????????????????????
???????????????? ???????????????????????????????
??? awk ????????????????? - ????????
- ????? pattern matching
- ????????????????
- ??????????
4Program Structure
awk ??????????????????????????????
?????? Pattern Action Pattern Action .
. . - awk ????????????????????????????????????
?????????? ???????? record ??? field -
??????????????????? pattern ??? action ???
5Lexical Units
Awk ??????????????????? ?????????????????????????t
okens ?????? - Numeric constants - String
Constants - Keyword - Identifiers -
Operators - Record and Fields - Comment -
Token used for grouping
6Lexical Units
Numeric constants ???????????????????????????????
? ??????????????????????????? ????????????????????
?????? ???? 1.2 0.12 ???? 1.2e2 String
constants ?????????????????????????????????? ???
?????? ????????? ???????????
??????????????? ?????? \ ???? hello
7Lexical Units
8Lexical Units
Identifier ??????????????????????????????????
?????????????????????????????? (underscore)
??????????????????? ???? array ???????????????????
??????????????? _ ???? var1 ???? _var1
?????????????????????????????????????????????
???? Var1 ??? var1
9Lexical Units
Operators awk ?? operator ??????????????? -
Assignment operators - Arithmetic operators -
relational operators - logical operator -
Regular expression matching operators
10Lexical Units
Assignment operators
11Lexical Units
Arithmetic operators
12Lexical Units
Relational operators
13Lexical Units
Logical operators
Regular expression matching operators
14Lexical Units
Record and Fields token 0 ?????????????
record ???? i ????????????? ??? field ???
i ???? Sunti Chotkaew 4322066 0 Sunti
Chotkaew 4322066 1 Sunti , 2 Chotkaew ,
3 4322066 ???????????????????????? NF
????????????? ????? field ?? record ???? NF
????????????? field ????????? record ???? NR
????????????????????? record ???????
15Lexical Units
Comment ???????? comment ????????????????
??????????????? comment ???????????? Token used
for grouping ????? token ?? awk ?????????????
blank , tabs ???? newline ???????????????????
???? ?????????? Action / / ??????????
Pattern ?????????? String
16Primary expression
?? action ??? pattern ??? awk ?????????????
block ??? expression ??????????????????
expression ???????? - Numeric constants -
String constant - variables - Function ???
expression ??????????????????? ????????????? ???
string ?????????????????
17Primary expression
Numeric Constant ???????????????????????????
18Primary expression
String Constant ???????????????????????????
19Primary expression
Variables ?????????????? identifiers ????????
???? identifierexpression ??????
array Term ????????????? record ??????
20Primary expression variables
Term - Term ???????????????????????????? 0
??????? field ??? i - ??? i ???????????????????
term ??? i lt 0 ??? i gt 100 awk ?????????
error ??? NF lt i lt 100 i ????????????????????
????????? - ?????????? i ??? i gt NF
????????????? NF ???????
21Primary expression
Function awk ?? function ?????? ??? arithmetic
??? String Function
22Primary expression
23Term
??????????????????????????? ??????? expression
???????????????????? ?????????????????????????????
term ????????????????? Primary expression term
binop term unop term incremented
variable (term)
24Term
Binary term ????????? term binop term binop
, - , , / , Unary term ????????? unop
term unop , -
25Term
Incremented variable ???? term ???????????
???????????? ?????? ??? ??????? var var
1 var var 1 -- var var - 1 var var -
1 var var var var 1 var -- var var
var - 1 Parenthesized term ???????? group
term
26Expression
Expression ?????????????? term term term
... var asgnop expression concatenation of
term ??????????? term term1 term2
?????? string ????????? concatenate
?????????????? 12 34 37
27Expression
Assignment expression ????????? var asgnop
expression ????? asgnop , , - , , /
, ?????? var op expression ????????
var var op expression ????? op , , _ ,
, / , ??? asgnop ???? right association
28Using awk
???????????????????? awk ??????????????? -
Input and output - Pattern - Action -
Special feature
29Using awk
Input and output ????????????? awk
program????????????? 1. Command line
?????????????? awk program file1
??????????? program ?????? argument ??????
awk ???????????? ???????????????? program
?????? argument ?????? awk ???????????? awk
/x/ print file1
30Using awk Input and output
2. ?????????????? awk program ????? file
???????? program ???????????????
????????????????? ??????????????????? ????
????????? awkprog ??? ?????????????? option
-f ??????????????????????? awk -f awkprog file1
31Using awk Input and output Input
Input Record and Fields awk ?????? input
??????????? record ????????? new line character
???? EOF ???????????????????????????? 0 awk
??????????? record ????????????????????? fields
??????????? blank ???? tabs ????????
????????????????????? ????????????????????????????
???? record ??????? Fields ???
32Using awk Input and output Input
Sample input file countries Russia 8650 262 As
ia Canada 3852 24 North America China 3692 866 A
sia USA 3615 219 North America Brazil 3286 116 S
outh America Australia 2968 14 Australia India 12
69 637 Asia Argentina 1072 26 South
America Sudan 968 19 Africa Algeria 920 18 Afric
a
33Using awk Input and output Input sample
input file
????????????????? blank ????????????????????????
North ??? America ???????????????? blank ???? tab
????????? field ????????????? record ?? 4 ???? 5
fileds ????????????? record ????????????????????
????????? program ?????? print 0 ??????
field ???? ? ??????????? ????? 1 , 2 , 3 ???
4 ????????
34Using awk Input and output Input
Input from command line ?? awk program
????????????????????????????? ???? x 5
?????????????????? awk print x x5
- ??????????????? x ?????? x5 ?????? input ???
standard input ????????????????? - ????????????
file ??????
35Using awk Input and output Input from
command line
?????????????????????? RS (Record separator) ???
FS (Field separator) ??????????????? command line
?????? awk -f awkprog RS file1 ????
awk -F -f awkprog file1
36Using awk Input and output Output
Output Printing ?????????????????????
?????????????? ?????????????? program ????????
awk print 1,2 ,3 countries ????
semicolon ????????????????????????????? x 5
print x ( ... ) ????????????????
???? print (1,2)
37Using awk Input and output Output
????????????????????????????????? output
???????????? NR ??????????????? record
??????? NF ??????? ???????? fileds ?? record
???? ????????????????????? print NR , NF ,
0
38Using awk Input and output Output
??? OFS ????????????????????????? field
?????????????? ??????????????? blank ORS
????????????????????????? record
????????? ????? ?????????? newline
charecter ????????????????????? x hello
y, world print x ,y ???????????
hello, world
39Using awk Input and output Output
Output to Different file awk
????????????????????????????? file
????????????????????????????? print gt
filename ??????????????????????????? 10 file
???????? ???????????? if (4 Asia)
print gt ASIA if (4 Europe) print gt
EUROPE if (4 North) print gt
NORTH_AMERICA if (4 South) print gt
SOUTH_AMERICA if (4 Africa) print gt
AFRICA
40Using awk Input and output Output
Output to Pipes ????????????????? output
????? pipe ?????? ?????????? ???????????? if
(2 xx) print mailx mary ???? print
1 sort ???? print cat -v /dev/tty
41Using awk
Pattern pattern ?????????????????????????????
????? action ??????????? expression ??????
pattern ?????? - Certain keyword - Arithmetic
relational expression - Regular expression -
Combination of these
42Using awk Pattern
BEGIN and END BEGIN ??? END ???? pattern
??????????? BEGION ??????? match
??????????????? input ??????? record
???????????? END ??????? match
?????????????? input ??????? record
?????????????? ?????????????? ??? BEGIN print
Country , Area , Population , Continent
print
43Using awk Pattern
Relational expression ?????? expression
?????????????????????????? pattern ???? 3 gt
1000 ???? 4 Asia print 1 Operator
??????????????????????? lt , lt , ,gt ,gt , !
44Using awk Pattern
Regular expression awk ????????????????????????
string ??????????? regular expression
??????????????????? / / ???? pattern Match
string /A/ ??????????? A /ia/ ??????????
ia /xy/ ?????????? x ???? y /axb/ axb ,
axxb /ax?b/ ab , axb /a.b/ axb , aPb ,
aXb /axb/ ab , axb , axxb
45Using awk Pattern
Combination of these ?????????????? pattern
?????????????????????????????? operators (AND)
, (OR) ???? ! (NOT) ???? 2 gt 3000 3 gt
100 4 Asia 4 Africa 4
/(AsiaAfrica)/ ??????? pattern
????????????????????????
46Using awk Pattern
Pattern range pattern ???????????????
pattern1 , pattern2 action awk
??????????????? record ??????????????
?????????????????????????????????????? pattern1
??????????????? ????????????????????? pattern2
???? /Canada/ , /Brazil/ NR 2 , NR 5
47Using awk
Action action ???????????????????????????????????
???? ????????????????? ???? ?????? semicolon ()
Variables , Expression and Assignment awk
??????????????????????????????????????????????????
??????????????????????? ???? /Asia/ pop 3
n END print total population of , n ,
Asian countries is , pop
48Using awk Action
Initialized of variables ????????????????????????
?????????????????????????????????? pop ??? n
????????????????????????????????
????????????????? ????????????????
0 ??????????? string ???????????????? null
string ?????????????????????????????????????????
? program ????????? maxpop lt 3 maxpop
3 country 1 END print country ,
maxpop
49Using awk Action
Field variable ???????????????????????????????
fields ????????????? 2 / 1000 print ????
BEGIN FS \t 4 1000 3 / 2
print ???? /USA/ 1 United State
print
50Using awk Action
String concatenation ??????????? string ??
concatenate ???????????????????? expression
?????? ???? x hello x x ,
world print x ???????????????? hello,
world
51Using awk Action
Special variables
52Using awk Action
Type type ????????? ??????????? context ??????
pop 3 pop ?? type ???? number country
1 country ?? type ???? string maxpop lt 3
maxpop ???? type ??? 3 ????????????? type
????? set ?????? assign ??? v expr ??? expr
???? arithmetic v ???? number ??? expr ????
concatenation v ???? string ??????????????? type
???????? ???? number expr 0
string expr
53Using awk Action
Array ???????????? array 1 ???? ?? awk ??? ????
XNR 0 ???????????? ??? index ???? -
numeric value - non numeric value
???????????? /Asia/ popAsia 3
54Using awk
Special feature ??????????? feature
?????????????????????? awk ??? - Bulit-In
Function - Flow of control - Report
generator - Cooperation with the shell -
Multidimensional arrray
55Using awk Special feature
Built-In function length ??????????????? string
???? print length,0 split ???????? field
??????? array ??????? filed ?????? ?????
???????????????? FS split ( S,array,sep) substr ?
?????????????????? string ???????? ???
??????? ???????? substr (s , m , n)
56Using awk Special feature Bulit-In function
Index ????? return ????????????????? s2 ???????
s1 ??? ?????????? return 0 index
(s1,s2) sprintf ?????????????????? printf
statement ??????????? ??????????? x
sprintf(10s 6d,1,2)
57Using awk Special feature
Flow of control awk ?? control statement
???????????? - if ... else - while - for
58Using awk Special feature Flow of control
If Else ?????????????? if (condition)
statement1 else statement2 ???????? if
(maxpop lt 3) maxpop 3 country 1
END print country , maxpop
59Using awk Special feature Flow of control
While ?????????????? while (condition)
statement ???????? i1 while (i lt NF)
print i i
60Using awk Special feature Flow of control
For ?????????????? for (expr1,expr2,expr3)
statement statement2 ???????? for (i 1i
lt NFi) print i
61Using awk Special feature Flow of control
?? statement ????????????? control statement ???
break ???????? loop ????? continue
??????????????????? loop next ????????????
record ????? ????????????? ????????????? exit
??????????????? ??? ???????? BEGIN
???????????????? body ????????? END END
??????????????????????
62Using awk Special feature
Report generator ?????????????????? ??????
Sunti awk 9 Ying nroff 8 Nop make 7 Dare dbx
5 Ying troff 2 Sunti awk 1 Dare xdbx 5 Nop mai
l 3
63Using awk Special feature
Cooperation with the shell ??????????????????
filed ???????????????????? awk print
n ????????????? execute field ?????? field
n ?????????????????? n ????????????? awk
print 1 ??? awk print \ 1
64Using awk Special feature
Multidimensional array ?????????????????????????
multidimensional array ???????????? for ( i 1
i lt 10 i ) for (j 1 j lt 10
j) multi , j
65Reference
Fortgang, Karen S., UNIX System V (Programmers
Guide), Prentice Hall, Inc., Englewood Cliffs,
NS 67632, 1987. Burk, Robin and Horvath,
David B., CCP, et al., UNIX Unleashed Internet
Edition, SAM Publishing ,1998