Lexical Units Awk tokens Numeric constants String Constants

  • Slides: 65
Download presentation

Lexical Units Awk โปรแกรมจะประกอบดวย กลมของอกขระทเรยกวา tokens ดงน - Numeric constants - String Constants -

Lexical Units Awk โปรแกรมจะประกอบดวย กลมของอกขระทเรยกวา tokens ดงน - Numeric constants - String Constants - Keyword - Identifiers - Operators - Record and Fields - Comment - Token used for grouping

Lexical Units

Lexical Units

Lexical Units Operators awk ม operator ใหใชไดดงน -Assignment operators - Arithmetic operators - relational

Lexical Units Operators awk ม operator ใหใชไดดงน -Assignment operators - Arithmetic operators - relational operators - logical operator - Regular expression matching operators

Lexical Units Assignment operators

Lexical Units Assignment operators

Lexical Units Arithmetic operators

Lexical Units Arithmetic operators

Lexical Units Relational operators

Lexical Units Relational operators

Lexical Units Logical operators Regular expression matching operators

Lexical Units Logical operators Regular expression matching operators

Primary expression Numeric Constant ตารางตวอยางคาของคาคงท

Primary expression Numeric Constant ตารางตวอยางคาของคาคงท

Primary expression String Constant ตารางตวอยางคาของคาคงท

Primary expression String Constant ตารางตวอยางคาของคาคงท

Primary expression Variables มรปแบบดงน ตวแปรธรรมดา identifier[expression] ตวแปร array $Term ตวแปรอางถง identifiers ขอมล record

Primary expression Variables มรปแบบดงน ตวแปรธรรมดา identifier[expression] ตวแปร array $Term ตวแปรอางถง identifiers ขอมล record

Primary expression Function awk ม function ใหใช String Function คอ arithmetic และ

Primary expression Function awk ม function ใหใช String Function คอ arithmetic และ

Primary expression

Primary expression

Term Binary term อยในรป term binop : + , - , * , /

Term Binary term อยในรป term binop : + , - , * , / , % Unary term อยในรป unop term unop : + , -

Term Incremented variable เปน term ทมรปแบบ คา ++ var -- var ++ var --

Term Incremented variable เปน term ทมรปแบบ คา ++ var -- var ++ var -- Parenthesized term var + 1 var - 1 var ใช ในการ group term และคาดงน ผลลพธ var = var + 1 var = var - 1

Expression มรปแบบดงน term. . . var asgnop expression concatenation of term ในรปแบบของ term 1

Expression มรปแบบดงน term. . . var asgnop expression concatenation of term ในรปแบบของ term 1 term 2 … คาของ string จะถกนำมา กนดงตว อยาง 1+2 3+4 = 37 concatenate

Expression Assignment expression รปแบบของ เมอ var asgnop expression asgnop : = , += ,

Expression Assignment expression รปแบบของ เมอ var asgnop expression asgnop : = , += , -= , *= , /= , %= รปแบบ var = op expression สมนยกบ var = var op expression เมอ op : = , + , _ , * , / , % และ asgnop เปน right association

Using awk เราจะอธบายการใชงาน ตามหวขอดงน awk - Input and output - Pattern - Action -

Using awk เราจะอธบายการใชงาน ตามหวขอดงน awk - Input and output - Pattern - Action - Special feature

Using awk : Input and output : Input Sample input file : countries Russia

Using awk : Input and output : Input Sample input file : countries Russia Canada China USA Brazil Australia India Argentina Sudan Algeria 8650 3852 3692 3615 3286 2968 1269 1072 968 920 262 24 866 219 116 14 637 26 19 18 Asia North America South America Australia Asia South America Africa

Using awk : Input and output : Input : from command line เราสามารถเปลยนคาของ RS

Using awk : Input and output : Input : from command line เราสามารถเปลยนคาของ RS (Record separator) และ FS (Field separator) โดยการคำสงจาก command line ดงน awk -f awkprog RS = “; ” file 1 หรอ awk -F; -f awkprog file 1

Using awk Pattern pattern เปนการเลอกตดสนใจสำหรบการ กระทำ action โดยทเรานำ expression มาเปน pattern ดงน - Certain

Using awk Pattern pattern เปนการเลอกตดสนใจสำหรบการ กระทำ action โดยทเรานำ expression มาเปน pattern ดงน - Certain keyword - Arithmetic relational expression - Regular expression - Combination of these

Using awk : Action String concatenation เราสามารถนำ string มา concatenate กนไดโดยการเขยนให expression ตดกน {

Using awk : Action String concatenation เราสามารถนำ string มา concatenate กนไดโดยการเขยนให expression ตดกน { x = “hello” x = x “, world” print x } ผลลพธทไดคอ hello, world เชน

Using awk : Action Special variables

Using awk : Action Special variables

Using awk : Action Type ดงน type ของตวแปร ขน อยกบ context pop = $3

Using awk : Action Type ดงน type ของตวแปร ขน อยกบ context pop = $3 pop ม type เปน number country = $1 country ม type เปน string maxpop < $3 maxpop จะม type ตาม $3 โดยทวไปแลว type จะถก set จากการ assign คา v = expr ถา expr เปน arithmetic v เปน number ถา expr เปน concatenation v เปน string สำหรบการบงคบ type ทำไดคอ เปน number :

Using awk Special feature เราจะพดถง feature พเศษทเราสามารถใชใน awk ได - Bulit-In Function - Flow

Using awk Special feature เราจะพดถง feature พเศษทเราสามารถใชใน awk ได - Bulit-In Function - Flow of control - Report generator - Cooperation with the shell - Multidimensional arrray

Using awk : Special feature Flow of control awk ม control statement ให ใชดง

Using awk : Special feature Flow of control awk ม control statement ให ใชดง น - if. . . else - while - for

Using awk : Special feature : Flow of control If … Else มรปแบบดงน if

Using awk : Special feature : Flow of control If … Else มรปแบบดงน if (condition) statement 1 else statement 2 ตวอยาง { if (maxpop < $3) { maxpop = $3 country =$1 } } END { print country , maxpop }

Using awk : Special feature : Flow of control While มรปแบบดงน while (condition) statement

Using awk : Special feature : Flow of control While มรปแบบดงน while (condition) statement ตวอยาง { } i=1 while (i <= NF) { print $i ++i }

Using awk : Special feature : Flow of control For มรปแบบดงน for (expr 1,

Using awk : Special feature : Flow of control For มรปแบบดงน for (expr 1, expr 2, expr 3) statement 2 ตวอยาง { } for (i = 1; i <= NF; i++) print $i

Using awk : Special feature Report generator พจารณาจากตวอยาง Sunti Ying Nop Dare Ying Sunti

Using awk : Special feature Report generator พจารณาจากตวอยาง Sunti Ying Nop Dare Ying Sunti Dare Nop awk nroff make dbx troff awk xdbx mail 9 8 7 5 2 1 5 3 ดงน

Using awk : Special feature Multidimensional array เราสามารถจำลองการ ทำงาน ของ multidimensional array ไดดงนคอ for

Using awk : Special feature Multidimensional array เราสามารถจำลองการ ทำงาน ของ multidimensional array ไดดงนคอ for ( i = 1 ; i <= 10 ; i++ ) for (j = 1 j <= 10 ; j++) mult[i “, ” j] = …

Reference Fortgang, Karen S. , UNIX System V (Programmer’s Guide), Prentice Hall, Inc. ,

Reference Fortgang, Karen S. , UNIX System V (Programmer’s 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