Alfred V, Aho, Peter J. Weinberger, ??? Brian W. Kernighan ???????? awk ???? 1977 ???????????????????????????????????????????? ????????????????????????????????????? UNIX ??? C ??????????????? ??????????????????????? C ????? awk - PowerPoint PPT Presentation

About This Presentation
Title:

Alfred V, Aho, Peter J. Weinberger, ??? Brian W. Kernighan ???????? awk ???? 1977 ???????????????????????????????????????????? ????????????????????????????????????? UNIX ??? C ??????????????? ??????????????????????? C ????? awk

Description:

* Input : from command line awk program x = 5 ... – PowerPoint PPT presentation

Number of Views:195
Avg rating:3.0/5.0
Slides: 66
Provided by: Sun115
Category:

less

Transcript and Presenter's Notes

Title: Alfred V, Aho, Peter J. Weinberger, ??? Brian W. Kernighan ???????? awk ???? 1977 ???????????????????????????????????????????? ????????????????????????????????????? UNIX ??? C ??????????????? ??????????????????????? C ????? awk


1
(No Transcript)
2
A Brief history
Alfred V, Aho, Peter J. Weinberger, ??? Brian W.
Kernighan ???????? awk ???? 1977
????????????????????????????????????????????
????????????????????????????????????? UNIX ??? C
??????????????? ??????????????????????? C ?????
awk
3
Introduction to Awk
  • awk ?????????????????????????????????????????????
    ??????????????????????????????????????????????????
    ???????????????? ???????????????????????????????
    ??? awk ?????????????????
  • ????????
  • ????? pattern matching
  • ????????????????
  • ??????????

4
Program Structure
awk ??????????????????????????????
?????? Pattern Action Pattern Action .
. . - awk ????????????????????????????????????
?????????? ???????? record ??? field -
??????????????????? pattern ??? action ???
5
Lexical Units
Awk ??????????????????? ?????????????????????????t
okens ?????? - Numeric constants - String
Constants - Keyword - Identifiers -
Operators - Record and Fields - Comment -
Token used for grouping
6
Lexical Units
Numeric constants ???????????????????????????????
? ??????????????????????????? ????????????????????
?????? ???? 1.2 0.12 ???? 1.2e2 String
constants ?????????????????????????????????? ???
?????? ????????? ???????????
??????????????? ?????? \ ???? hello
7
Lexical Units
8
Lexical Units
Identifier ??????????????????????????????????
?????????????????????????????? (underscore)
??????????????????? ???? array ???????????????????
??????????????? _ ???? var1 ???? _var1
?????????????????????????????????????????????
???? Var1 ??? var1
9
Lexical Units
Operators awk ?? operator ??????????????? -
Assignment operators - Arithmetic operators -
relational operators - logical operator -
Regular expression matching operators
10
Lexical Units
Assignment operators
11
Lexical Units
Arithmetic operators
12
Lexical Units
Relational operators
13
Lexical Units
Logical operators
Regular expression matching operators
14
Lexical 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 ???????
15
Lexical Units
Comment ???????? comment ????????????????
??????????????? comment ???????????? Token used
for grouping ????? token ?? awk ?????????????
blank , tabs ???? newline ???????????????????
???? ?????????? Action / / ??????????
Pattern ?????????? String
16
Primary expression
?? action ??? pattern ??? awk ?????????????
block ??? expression ??????????????????
expression ???????? - Numeric constants -
String constant - variables - Function ???
expression ??????????????????? ????????????? ???
string ?????????????????
17
Primary expression
Numeric Constant ???????????????????????????
18
Primary expression
String Constant ???????????????????????????
19
Primary expression
Variables ?????????????? identifiers ????????
???? identifierexpression ??????
array Term ????????????? record ??????
20
Primary 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 ???????
21
Primary expression
Function awk ?? function ?????? ??? arithmetic
??? String Function
22
Primary expression
23
Term
??????????????????????????? ??????? expression
???????????????????? ?????????????????????????????
term ????????????????? Primary expression term
binop term unop term incremented
variable (term)
24
Term
Binary term ????????? term binop term binop
, - , , / , Unary term ????????? unop
term unop , -
25
Term
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
26
Expression
Expression ?????????????? term term term
... var asgnop expression concatenation of
term ??????????? term term1 term2
?????? string ????????? concatenate
?????????????? 12 34 37
27
Expression
Assignment expression ????????? var asgnop
expression ????? asgnop , , - , , /
, ?????? var op expression ????????
var var op expression ????? op , , _ ,
, / , ??? asgnop ???? right association
28
Using awk
???????????????????? awk ??????????????? -
Input and output - Pattern - Action -
Special feature
29
Using awk
Input and output ????????????? awk
program????????????? 1. Command line
?????????????? awk program file1
??????????? program ?????? argument ??????
awk ???????????? ???????????????? program
?????? argument ?????? awk ???????????? awk
/x/ print file1
30
Using awk Input and output
2. ?????????????? awk program ????? file
???????? program ???????????????
????????????????? ??????????????????? ????
????????? awkprog ??? ?????????????? option
-f ??????????????????????? awk -f awkprog file1
31
Using awk Input and output Input
Input Record and Fields awk ?????? input
??????????? record ????????? new line character
???? EOF ???????????????????????????? 0 awk
??????????? record ????????????????????? fields
??????????? blank ???? tabs ????????
????????????????????? ????????????????????????????
???? record ??????? Fields ???
32
Using 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
33
Using 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 ????????
34
Using awk Input and output Input
Input from command line ?? awk program
????????????????????????????? ???? x 5
?????????????????? awk print x x5
- ??????????????? x ?????? x5 ?????? input ???
standard input ????????????????? - ????????????
file ??????
35
Using 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
36
Using awk Input and output Output
Output Printing ?????????????????????
?????????????? ?????????????? program ????????
awk print 1,2 ,3 countries ????
semicolon ????????????????????????????? x 5
print x ( ... ) ????????????????
???? print (1,2)
37
Using awk Input and output Output
????????????????????????????????? output
???????????? NR ??????????????? record
??????? NF ??????? ???????? fileds ?? record
???? ????????????????????? print NR , NF ,
0
38
Using awk Input and output Output
??? OFS ????????????????????????? field
?????????????? ??????????????? blank ORS
????????????????????????? record
????????? ????? ?????????? newline
charecter ????????????????????? x hello
y, world print x ,y ???????????
hello, world
39
Using 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
40
Using awk Input and output Output
Output to Pipes ????????????????? output
????? pipe ?????? ?????????? ???????????? if
(2 xx) print mailx mary ???? print
1 sort ???? print cat -v /dev/tty

41
Using awk
Pattern pattern ?????????????????????????????
????? action ??????????? expression ??????
pattern ?????? - Certain keyword - Arithmetic
relational expression - Regular expression -
Combination of these
42
Using awk Pattern
BEGIN and END BEGIN ??? END ???? pattern
??????????? BEGION ??????? match
??????????????? input ??????? record
???????????? END ??????? match
?????????????? input ??????? record
?????????????? ?????????????? ??? BEGIN print
Country , Area , Population , Continent
print
43
Using awk Pattern
Relational expression ?????? expression
?????????????????????????? pattern ???? 3 gt
1000 ???? 4 Asia print 1 Operator
??????????????????????? lt , lt , ,gt ,gt , !
44
Using 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
45
Using awk Pattern
Combination of these ?????????????? pattern
?????????????????????????????? operators (AND)
, (OR) ???? ! (NOT) ???? 2 gt 3000 3 gt
100 4 Asia 4 Africa 4
/(AsiaAfrica)/ ??????? pattern
????????????????????????
46
Using awk Pattern
Pattern range pattern ???????????????
pattern1 , pattern2 action awk
??????????????? record ??????????????
?????????????????????????????????????? pattern1
??????????????? ????????????????????? pattern2
???? /Canada/ , /Brazil/ NR 2 , NR 5
47
Using awk
Action action ???????????????????????????????????
???? ????????????????? ???? ?????? semicolon ()
Variables , Expression and Assignment awk
??????????????????????????????????????????????????
??????????????????????? ???? /Asia/ pop 3
n END print total population of , n ,
Asian countries is , pop
48
Using awk Action
Initialized of variables ????????????????????????
?????????????????????????????????? pop ??? n
????????????????????????????????
????????????????? ????????????????
0 ??????????? string ???????????????? null
string ?????????????????????????????????????????
? program ????????? maxpop lt 3 maxpop
3 country 1 END print country ,
maxpop
49
Using awk Action
Field variable ???????????????????????????????
fields ????????????? 2 / 1000 print ????
BEGIN FS \t 4 1000 3 / 2
print ???? /USA/ 1 United State
print
50
Using awk Action
String concatenation ??????????? string ??
concatenate ???????????????????? expression
?????? ???? x hello x x ,
world print x ???????????????? hello,
world
51
Using awk Action
Special variables
52
Using 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
53
Using awk Action
Array ???????????? array 1 ???? ?? awk ??? ????
XNR 0 ???????????? ??? index ???? -
numeric value - non numeric value
???????????? /Asia/ popAsia 3
54
Using awk
Special feature ??????????? feature
?????????????????????? awk ??? - Bulit-In
Function - Flow of control - Report
generator - Cooperation with the shell -
Multidimensional arrray
55
Using 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)
56
Using awk Special feature Bulit-In function
Index ????? return ????????????????? s2 ???????
s1 ??? ?????????? return 0 index
(s1,s2) sprintf ?????????????????? printf
statement ??????????? ??????????? x
sprintf(10s 6d,1,2)
57
Using awk Special feature
Flow of control awk ?? control statement
???????????? - if ... else - while - for
58
Using 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
59
Using awk Special feature Flow of control
While ?????????????? while (condition)
statement ???????? i1 while (i lt NF)
print i i
60
Using awk Special feature Flow of control
For ?????????????? for (expr1,expr2,expr3)
statement statement2 ???????? for (i 1i
lt NFi) print i
61
Using awk Special feature Flow of control
?? statement ????????????? control statement ???
break ???????? loop ????? continue
??????????????????? loop next ????????????
record ????? ????????????? ????????????? exit
??????????????? ??? ???????? BEGIN
???????????????? body ????????? END END
??????????????????????
62
Using 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
63
Using awk Special feature
Cooperation with the shell ??????????????????
filed ???????????????????? awk print
n ????????????? execute field ?????? field
n ?????????????????? n ????????????? awk
print 1 ??? awk print \ 1
64
Using awk Special feature
Multidimensional array ?????????????????????????
multidimensional array ???????????? for ( i 1
i lt 10 i ) for (j 1 j lt 10
j) multi , j
65
Reference
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
Write a Comment
User Comments (0)
About PowerShow.com