65

A Brief history

  • Upload
    huy

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

A Brief history. Alfred V, Aho, Peter J. Weinberger, และ Brian W. Kernighan ได้สร้าง awk ในปี 1977 โดยได้ชื่อมาจากตัวขึ้นต้นของชื่อของทั้งสามคน โดยบุคคลทั้งสามก็มาจากกลุ่มคนที่พัฒนา UNIX และ C โดยจะสามารถเห็น ไวยากรณ์ที่คล้ายคลึงกับ C ได้ใน awk. Introduction to Awk. - PowerPoint PPT Presentation

Citation preview

Page 1: A Brief history
Page 2: A Brief history

Alfred V, Aho, Peter J. Weinberger, และ Brian W. Kernighan ได้�สร้�าง awk ในปี� 197

7 โด้ยได้�ชื่��อมาจากตั�วขึ้��นตั�นขึ้องชื่��อขึ้องทั้��งสามคน โด้ยบุ"คคลทั้��งสามก#มาจากกล"$มคนทั้%�พั�ฒนา UNIX และ C โด้ยจะสามาร้ถเห็#น ไวยากร้ณ์,ทั้%�คล�ายคล�งก�บุ C ได้�ใน awk

A Brief history

Page 3: A Brief history

awk ค�อภาษาโปีร้แกร้มส/าห็ร้�บุปีร้ะมวลผลแฟ้2มขึ้�อม3ลทั้%�ถ3กออกแบุบุมาส/าห็ร้�บุการ้สร้�างขึ้�อม3ลทั้��วไปีและใชื่�ก�บุงานทั้%�

ใชื่�ในการ้ด้�งขึ้�อความ ซึ่��งง$ายส/าห็ร้�บุการ้ใชื่� และการ้ทั้/างาน โด้ย awk สามาร้ถทั้/าด้�งน%�

ค�อ- ทั้/าร้ายงาน- การ้ทั้/า pattern matching- การ้ตัร้วจสอบุขึ้�อม3ล- กร้องขึ้�อม3ล

Introduction to Awk

Page 4: A Brief history

awk โปีร้แกร้มจะม%ล�กษณ์ะเปี5นชื่"ด้ค/าส��ง ด้�งน%�

Pattern {Action}Pattern {Action}. . .

- awk จะทั้/างานก�บุชื่"ด้ขึ้�อม3ลขึ้องแฟ้2มขึ้�อม3ลเขึ้�า โด้ยทั้/างานใน ในร้3ปีแบุบุ record และ field- เร้าสามาร้ถละการ้เขึ้%ยน pattern และ action ได้�

Program Structure

Page 5: A Brief history

Awk โปีร้แกร้มจะปีร้ะกอบุด้�วย กล"$มขึ้องอ�กขึ้ร้ะทั้%�เร้%ยกว$าtokens ด้�งน%�

- Numeric constants- String Constants- Keyword- Identifiers- Operators- Record and Fields- Comment- Token used for grouping

Lexical Units

Page 6: A Brief history

Numeric constants

เปี5นชื่"ด้ขึ้องตั�วอ�กขึ้ร้ะทั้%�เปี5นตั�วเลขึ้ ม%ค$าเปี5นจ/านวนเตั#มและทั้ศน7ยม โด้ยทั้ศน7ยมเขึ้%ยนได้�ด้�งน%�ค�อเชื่$น 12 012 ห็ร้�อ 12. e2

String constants

เปี5นชื่"ด้ขึ้องตั�วอ�กขึ้ร้ะทั้%�เปี5นตั�วอ�กษร้ และ ตั�วเลขึ้ ม%ค$าเปี5น ล�อมร้อบุด้�วย “ “ ถ�าตั�องการ้พั7มพั, “ ให็�ใชื่� \“ เชื่$น “hello”

Lexical Units

Page 7: A Brief history

Lexical Units

KeywordsBEGIN break log

END close nextFILENAME continue number

FS exit printNF exp printfNR for splitOFS getline sprintfORS if sqrt

OFMT in stringRS index substr

int whilelength

Page 8: A Brief history

Identifier

เปี5นชื่"ด้ขึ้องตั�วอ�กขึ้ร้ะทั้%�เปี5นตั�วอ�กษร้ ตั�วเลขึ้และเคร้��องห็มายขึ้%ด้เส�นใตั� (underscore) ใชื่�ส/าห็ร้�บุเปี5นตั�วแปีร้ ห็ร้�อarray โด้ยชื่��อตั�องขึ้��นตั�นด้�วยตั�วอ�กษร้ห็ร้�อ _ เชื่$น var1 ห็ร้�อ _var1 ตั�วอ�กษร้ตั�วให็ญ่$และตั�วเล#กความห็มายไม$เห็ม�อนก�น เชื่$น Var1 และ var1

Lexical Units

Page 9: A Brief history

Operators

awk ม% operator ให็�ใชื่�ได้�ด้�งน%�- Assignment operators- Arithmetic operators- relational operators- logical operator - Regular expression matching operators

Lexical Units

Page 10: A Brief history

Assignment operators

Lexical Units

Symbol Usage Description

= assignment

+= Plus-equals X+=Y : X = X + Y

-= minus-equals X -=Y : X = X – Y

*= times-equals X *= Y : X = X * Y

/= divide-equals X /= Y : X = X / Y

%= mod-equals X %= Y : X = X %Y

++ prefix and postfixincrements

++X, X++ : X = X+ 1

-- prefix and postfixdecrements

--X, X-- : X = X – 1

Page 11: A Brief history

Arithmetic operators

Lexical Units

Symbol Description

+ unary and binary plus

- unary and binary minus

* multiplication

/ division

% modulus

(…) grouping

Page 12: A Brief history

Relational operators

Lexical Units

Symbol Description

< less than

<= less than or equal to

== equal to

!= not equal to

>= greater than or equal to

> greater than

Page 13: A Brief history

Logical operators

Lexical Units

Symbol Description

&& and

| or

! not

Regular expression matching operators

Symbol Description~ matches

!~ does not matches

Page 14: A Brief history

Record and Fields token

$0 : ตั�วแปีร้เก#บุค$า record น��น$i : ตั�วแปีร้เก#บุค$า ขึ้อง field ทั้%� i

เชื่$น Sunti Chotkaew4322066

$0 = Sunti Chotkaew4322066

$1 = Sunti , $2 = Chotkaew , $3 = 4322066ตั�วแปีร้พั7เศษทั้%�เก%�ยวขึ้�อง

NF : ตั�วแปีร้เก#บุค$า จ/านวน field ใน record น��น

$NF : ตั�วแปีร้เก#บุค$า field ส"ด้ทั้�ายใน record น��น

NR : ตั�วแปีร้ทั้%�เก#บุค$าจ/านวน record ขึ้ณ์ะน��น

Lexical Units

Page 15: A Brief history

Comment

การ้เขึ้%ยน comment จะใชื่� เคร้��องห็มาย # น/าห็น�าส$วนทั้%�จะ

comment ไปีจนจบุบุร้ร้ทั้�ด้

Token used for grouping

แตั$ละ token ใน awk จะถ3กแบุ$งด้�วย blank , tabs ห็ร้�อ newline ห็ร้�อ

เคร้��องห็มายอ��น เชื่$น{ … } ใชื่�ล�อมร้อบุ Action/ … / ใชื่�ล�อมร้อบุ Pattern“ … “ ใชื่�ล�อมร้อบุ String

Lexical Units

Page 16: A Brief history

ใน action และ pattern ขึ้อง awk ถ3กสร้�างมาจาก block ขึ้อง expression ซึ่��งปีร้ะกอบุขึ้��นด้�วย expression ตั$อไปีน%�

- Numeric constants- String constant- variables- Function

โด้ย expression ขึ้�างตั�นสามาร้ถให็�ค$า ได้�ทั้� �งตั�วเลขึ้ และ string อย$างใด้อย$างห็น��ง

Primary expression

Page 17: A Brief history

Numeric Constant

ตัาร้างตั�วอย$างค$าขึ้องค$าคงทั้%�

Primary expression

NumericConstant

NumericValue

StringValue

0 0 0

1 1 1

.5 0.5 .5

.5e2 50 50

Page 18: A Brief history

String Constant

ตัาร้างตั�วอย$างค$าขึ้องค$าคงทั้%�

Primary expression

StringConstant

NumericValue

StringValue

“” 0 empty space

“a” 0 a

“XYZ” 0 XYZ

“o” 0 0

“1” 1 1

“.5” 0.5 .5

“.5e2” 0.5 .5e2

Page 19: A Brief history

Variables

ม%ร้3ปีแบุบุด้�งน%�

identifiers ตั�วแปีร้ธร้ร้มด้าidentifier[expression] ตั�วแปีร้ array$Term ตั�วแปีร้อ�างถ�ง

record ขึ้�อม3ล

Primary expression

Page 20: A Brief history

$Term

- Term เปี5นค$าตั�วเลขึ้ทั้%�ม%ค$ามากกว$า 0 อ�างถ�ง field ทั้%� i

- ให็� i เปี5นค$ามากทั้%�ส"ด้ขึ้อง term ถ�า i < 0 และ i > 100 awk จะ

ตัร้วจสอบุ error ถ�า NF < i < 100 $i จะเปี5น

ตั�วแปีร้ทั้%�ย�งไม$ก/าห็นด้ค$า- การ้เขึ้�าถ�ง $i ทั้%� i > NF จะไม$ทั้/าให็�

ค$า NF เปีล%�ยน

Primary expression : variables

Page 21: A Brief history

Function

awk ม% function ให็�ใชื่� ค�อ arithmetic และ String Function

Primary expression

Arithmetic Functions

exp (expression)int (expression)log (expression)sqrt (expression)

Page 22: A Brief history

Primary expression

String Functions

getlineindex (expression1,expression2)length (expression)split (expression,identifier,expression2)split (expression, identifier)sprintf (format, expression1,expression2..)substr (expression1,expression2)substr (expression1,expression2, expression3)

Page 23: A Brief history

ส/าห็ร้�บุการ้ทั้/างานทั้างคณ์7ตัศาสตัร้, จะน/าเอา expression มาทั้/าการ้สร้�างกล"$มขึ้อง ไวยากร้ณ์,ทั้%�ม%ขึ้นาด้ให็ญ่$เร้%ยกว$า term โด้ยม%ร้3ปีแบุบุด้�งน%�

Primary expressionterm binop term unop termincremented variable(term)

Term

Page 24: A Brief history

Binary term

อย3$ในร้3ปีterm binop term

binop : + , - , * , / , %

Unary term

อย3$ในร้3ปีunop term

unop : + , -

Term

Page 25: A Brief history

Incremented variable

เปี5น term ทั้%�ม%ร้3ปีแบุบุ และค$าด้�งน%�รู�ปแบบ ค่�า ผลล�พธ์�++ var var + 1 var = var + 1-- var var - 1 var = var - 1var ++ var var = var + 1var -- var var = var - 1

Parenthesized term

ใชื่�ในการ้ group term

Term

Page 26: A Brief history

Expression ม%ร้3ปีแบุบุด้�งน%� termterm term ...var asgnop expression

concatenation of term

ในร้3ปีแบุบุขึ้อง term term1 term2 …

ค$าขึ้อง string จะถ3กน/ามา concatenate ก�นด้�งตั�วอย$าง

1+2 3+4 = 37

Expression

Page 27: A Brief history

Assignment expression

ร้3ปีแบุบุขึ้อง var asgnop expression

เม��อ asgnop : = ,+= , -= , *= , /= , %=

ร้3ปีแบุบุvar = op expression

สมน�ยก�บุ var = var op expression

เม��อ op : = , + , _ ,* , / , % และ asgnop เปี5น right association

Expression

Page 28: A Brief history

เร้าจะอธ7บุายการ้ใชื่�งาน awk ตัามห็�วขึ้�อด้�งน%�

- Input and output- Pattern- Action - Special feature

Using awk

Page 29: A Brief history

Input and output

เร้าสามร้ถเขึ้%ยน awk programได้�สองว7ธ%ค�อ

1. Command line ม%ร้3ปีแบุบุด้�งน%�

awk ‘program’ file1

โด้ยทั้%�จะร้�บุ program มาเปี5น argument แร้กขึ้อง awk โด้ยทั้%�เร้าใชื่� ‘ ‘ เพั��อให็�แน$ใจว$า program จะเปี5น

argument แร้กขึ้อง awkตั�วอย$างเชื่$น

awk ‘/x/ {print}’ file1

Using awk

Page 30: A Brief history

2. เร้าสามาร้ถเขึ้%ยน awk program ไว�ใน file ได้�เม��อ program ขึ้องเร้าม%ขึ้นาด้ยาว และเร้าย�งสามาร้ถน/า เอากล�บุมาใชื่�ให็ม$ได้� เชื่$น เก#บุไว�ใน awkprog เร้า เร้%ยกโด้ยการ้ใชื่� option -f โด้ยสามาร้ถเร้%ยกใชื่�ด้�งน%�

awk -f awkprog file1

Using awk : Input and output

Page 31: A Brief history

Input : Record and Fields

awk จะอ$าน input ขึ้��นมาห็น��ง record โด้ยจบุด้�วย new line character ห็ร้�อ EOF โด้ยจะให็�ค$าทั้%�อ$านมาได้�ไปีทั้%� $0

awk จะทั้/าการ้แบุ$ง record ทั้%�อ$านได้�แบุ$งออกเปี5น fields โด้ยแบุ$งด้�วย blank ห็ร้�อ tabs

หมายเหต " เร้าสามาร้ถทั้/าการ้เปีล%�ยน อ�กขึ้ร้ะส/าห็ร้�บุทั้/าห็น�าทั้%�เปี5นตั�วแบุ$ง record และแบุ$ง Fields ได้�

Using awk : Input and output : Input

Page 32: A Brief history

Sample input file : countries

Russia 8650 262 AsiaCanada 3852 24 North AmericaChina 3692 866 AsiaUSA 3615 219 North AmericaBrazil 3286 116 South AmericaAustralia 2968 14 AustraliaIndia 1269 637 AsiaArgentina 1072 26 South AmericaSudan 968 19 AfricaAlgeria 920 18 Africa

Using awk : Input and output : Input

Page 33: A Brief history

จากตั�วอย$างเร้าใชื่� blank ห็น��งตั�วในการ้ขึ้��นร้ะห็ว$าง North ก�บุ America ทั้/าให็�เม��อเร้าใชื่� blank ห็ร้�อ tab ในการ้แบุ$ง field เร้าจะได้�แตั$ละ record ม% 4ห็ร้�อ 5 fileds ไม$เทั้$าก�นทั้"ก record

เม��อตั�องการ้พั7มพั,ค$า เร้าอาจใชื่� program ด้�งน%�

{ print $0 }

ส/าห็ร้�บุ field ตั$าง ๆ จะถ3กให็�ค$า ไปีทั้%� $1 , $2 , $3 และ $4 … ตัามล/าด้�บุ

Using awk : Input and output : Input : sample input file

Page 34: A Brief history

Input : from command line

ใน awk program เร้าสามาร้ถปีร้ะกาศตั�วแปีร้ได้�ทั้�นทั้% เชื่$น

x = 5

และเขึ้%ยนเปี5นค/าส��งawk ‘{print x}’ x=5 -

เปี5นการ้ก/าห็นด้ค$า x โด้ยให็� x=5 และร้�บุ input จาก standard input โด้ยใชื่�เคร้��องห็มาย - แทั้นการ้ร้�บุจาก file ขึ้�อม3ล

Using awk : Input and output : Input

Page 35: A Brief history

เร้าสามาร้ถเปีล%�ยนค$าขึ้อง RS (Record separator) และ FS (Field separator) โด้ยการ้ค/าส��งจาก command line ด้�งน%�

awk -f awkprog RS = “;” file1

ห็ร้�อ awk -F; -f awkprog file1

Using awk : Input and output : Input : from command line

Page 36: A Brief history

Output : Printing

จากแฟ้2มขึ้�อม3ลตั�วอย$าง เร้าสามาร้ถพั7มพั, ค$าออกมาโด้ยใชื่� program ตั$อไปีน%�

awk ‘{print $1,$2 ,$3}’ countries

ส$วน semicolon เร้าจะใชื่�ในการ้แบุ$งค/าส��งด้�งน%�

{x = 5 ; print x }( ... ) ใชื่�ในการ้จ�ด้กล"$ม เชื่$น

{print ($1,$2)}

Using awk : Input and output : Output

Page 37: A Brief history

เร้าสามาร้ถใชื่�ตั�วแปีร้พั7เศษในการ้พั7มพั, output ออกมาได้�เชื่$น

NR เก#บุค$าจ/านวนชื่อง record ขึ้ณ์ะน��น

NF เก#บุค$า จ/านวนขึ้อง fileds ใน record น��น

ตั�วอย$างการ้ใชื่�งานเชื่$น { print NR , NF , $0 }

Using awk : Input and output : Output

Page 38: A Brief history

และ OFS เก#บุอ�กขึ้ร้ะทั้%�ใชื่�ในการ้แบุ$ง

field ทั้%�เปี5นผลล�พัธ,ปีกตั7จะม%ค$าเปี5น blank

ORS เก#บุอ�กขึ้ร้ะทั้%�ใชื่�ในการ้แบุ$ง record ทั้%�เปี5นผล ล�พัธ, ปีกตั7จะเปี5น newline charecter

ตั�วอย$างการ้ใชื่�งานเชื่$น { x= “hello” ; y=“, world”

print x ,y }

ผลทั้%�ได้�ค�อ hello, world

Using awk : Input and output : Output

Page 39: A Brief history

Output : to Different file

awk อน"ญ่าตัให็�ม%การ้ส$งผลล�พัธ,ไปีย�ง file ได้�โด้ยสามาร้ถเขึ้%ยนค/าส��งด้�งน%�

print > “filename”โด้ยสามาร้ถทั้/าการ้เปี<ด้ได้�ส3งส"ด้ 10 file เทั้$าน��นตั�วอย$างเชื่$น { if ($4 == “Asia”) print > “ASIA” if ($4 == “Europe”) print > “EUROPE” if ($4 == “North”) print > “NORTH_AMERICA” if ($4 == “South”) print > “SOUTH_AMERICA” if ($4 == “Africa”) print > “AFRICA”}

Using awk : Input and output : Output

Page 40: A Brief history

Output : to Pipes

เร้าสามาร้ถทั้/าการ้ส$ง output ไปีย�งpipe ได้�โด้ย ม%ตั�วอย$าง ค/าส��งด้�งน%�

{ if ($2 == ‘xx’) print | “mailx mary” ห็ร้�อ { print $1 | “sort” }ห็ร้�อ { print … | “cat -v /dev/tty” }

Using awk : Input and output : Output

Page 41: A Brief history

Pattern

pattern เปี5นการ้เล�อกตั�ด้ส7นใจส/าห็ร้�บุการ้ กร้ะทั้/า action โด้ยทั้%�เร้าน/า expression มาเปี5น pattern ด้�งน%�

- Certain keyword- Arithmetic relational

expression- Regular expression- Combination of these

Using awk

Page 42: A Brief history

BEGIN and END

BEGIN และ END เปี5น pattern พั7เศษโด้ยทั้%�

BEGION : จะทั้/าการ้ match ส$วนเร้7�มตั�นขึ้อง input ก$อนทั้%� record แร้กจะถ3กอ$าน

END : จะทั้/าการ้ match ส$วนส"ด้ทั้�ายขึ้อง input ห็ล�งจาก record ส"ด้ทั้�ายถ3กอ$าน

ตั�วอย$างการ้ใชื่� ค�อBEGIN {print “Country” , “Area” , “Population” , “Continent” }

{ print }

Using awk : Pattern

Page 43: A Brief history

Relational expression

เร้าใชื่� expression ในการ้เปีร้%ยบุเทั้%ยบุในการ้สร้�าง pattern เชื่$น

$3 > 1000 ห็ร้�อ $4 == “Asia”{print $1}

Operator ส/าห็ร้�บุการ้เปีร้%ยบุเทั้%ยบุค�อ < , <= , == ,>= ,> , !

Using awk : Pattern

Page 44: A Brief history

Regular expression

awk อ/านวยความสะด้วกในการ้ค�นห็า string โด้ยการ้เขึ้%ยน regular expression ไว�ภายในเคร้��องห็มาย / / เขึ้$น

pattern Match string /^A/ ขึ้��นตั�นด้�วย A/ia$/ ลงทั้�ายด้�วย ia/x|y/ ปีร้ะกอบุด้�วย x ห็ร้�อ y/ax+b/ axb , axxb /ax?b/ ab , axb/a.b/ axb , aPb , aXb/ax*b/ ab , axb , axxb

Using awk : Pattern

Page 45: A Brief history

Combination of these

เร้าสามาร้ถน/าเอา pattern เห็ล$าน%�มาเขึ้%ยนร้วมก�นได้�โด้ยใชื่� operators && (AND) , || (OR) ห็ร้�อ ! (NOT) เชื่$น

$2 > 3000 && $3 > 100$4 == “Asia” || $4 ==

“Africa”$4 ~ /^(Asia|Africa)$/ ซึ่��งสอง pattern ส"ด้ทั้�ายได้�ผลล�พัธ,

เทั้$าก�น

Using awk : Pattern

Page 46: A Brief history

Pattern range

pattern ทั้%�อย3$ในร้3ปีแบุบุ pattern1 , pattern2

{action}awk จะทั้/างานโด้ยทั้/าก�บุ record ทั้%�

อย3$ร้ะห็ว$าง บุร้ร้ทั้�ด้ทั้%�เร้7�มทั้%�บุร้ร้ทั้�ด้ทั้%�ม%ค$าปีร้ากฏใน pattern1 จนการ้กร้ะทั้��งถ�ง บุร้ร้ทั้�ด้ทั้%�ม%ค$าปีร้ากฏใน pattern 2 เขึ้$น

/Canada/ , /Brazil/NR == 2 , NR == 5

Using awk : Pattern

Page 47: A Brief history

Action

action เปี5นชื่"ด้ขึ้องค/าส��งทั้%�แตั$ละค/าส��งจะแยกด้�วย การ้ขึ้��นบุร้ร้ทั้�ด้ให็ม$ ห็ร้�อ การ้ใชื่� semicolon (;)

Variables , Expression and Assignment

awk สน�บุสน"นการ้ทั้/างานทั้างคณ์7ตัศาสตัร้,และเก#บุค$าลงในตั�วแปีร้เพั��อน/าเอามาใชื่�ในภายห็ล�ง เชื่$น

/Asia/ { pop += $3 ; ++ n }END { print “total population of ”, n , “Asian

countries is “ , pop }

Using awk

Page 48: A Brief history

Initialized of variables

ในตั�วอย$างก$อนห็น�าน%�เร้าไม$ได้�ทั้/าการ้ให็�ค$าเร้7�มตั�นก�บุตั�วแปีร้ pop และ n แตั$โปีร้แกร้มย�งทั้/างานถ3กเน��องมาจาก

ถ�าเปี5นชื่น7ด้ตั�วเลขึ้ จะถ3กตั��งค$าเปี5น 0

ถ�าเปี5นชื่น7ด้ string จะถ3กตั��งตั$าเปี5น null string

ด้�งน��นเร้าสามาร้ถให็�ค$าทั้%�ก/าห็นด้ไว�แล�วเขึ้%ยน program ได้�ด้�งน%�

maxpop < $3 {maxpop = $3country = $1 }

END { print country , maxpop }

Using awk : Action

Page 49: A Brief history

Field variable

เร้าสามาร้ถให็�ค$าก�บุตั�วแปีร้ทั้%�เปี5น fields ได้�ด้�งน%�เชื่$น

{ $2 /= 1000; print }ห็ร้�อ BEGIN { FS = “\t” }

{ $4 = 1000 * $3 / $2 ; print }ห็ร้�อ /USA/ { $1 = “United State” ; print }

Using awk : Action

Page 50: A Brief history

String concatenation

เร้าสามาร้ถน/า string มา concatenate ก�นได้�โด้ยการ้เขึ้%ยนให็� expression ตั7ด้ก�น เชื่$น

{ x = “hello” x = x “, world”

print x }

ผลล�พัธ,ทั้%�ได้�ค�อ hello, world

Using awk : Action

Page 51: A Brief history

Special variables

Using awk : Action

NR จ/านวนขึ้อง record ปี>จจ"บุ�นNF จ/านวนขึ้อง field ใ น record น��นFS ตั�วแบุ$ง field ส/าห็ร้�บุ input โด้ยปีกตั7แล�วจะ เปี5น blank ห็ร้�อ tabRS ตั�วแบุ$ง record ส/าห็ร้�บุ input ปีกตั7แล�วจะ เปี5น new line character$I เปี5นค$าขึ้อง filed ทั้%� I ส/าห็ร้�บุ record ปี>จจ"บุ�น$0 เปี5นค$าขึ้อง input recordOFS เปี5นตั�วแบุ$ง filed ส/าห็ร้�บุ output ปีกตั7แล�วเปี5น blankORS เปี5นตั�วแบุ$ง record ส/าห็ร้�บุ output ปีกตั7แล�วเปี5น new line characterOFMT เปี5นร้3ปีแบุบุส/าห็ร้�บุ พั7มพั,ตั�วเลขึ้เม�อใ ชื่� printf ปีกตั7แล�วจะ เปี5น %.6gFILENAME เปี5นขึ้��อ file input ส/าห็ร้�บุ awk ม%ปีร้ะโยชื่น,มากเน��องจาก ค/าส��ง awk อย3$ใ น

ร้3ปีแบุบุ awk –f program file1 file2 file3

Page 52: A Brief history

Type

type ขึ้องตั�วแปีร้ ขึ้��นอย3$ก�บุ context ด้�งน%�

pop = $3 pop ม% type เปี5น number

country = $1 country ม% type เปี5น string

maxpop < $3 maxpop จะม% type ตัาม $3 โด้ยทั้��วไปีแล�ว type จะถ3ก set จากการ้ assign ค$า

v = expr ถ�า expr เปี5น arithmetic v เปี5น number

ถ�า expr เปี5น concatenation v เปี5น stringส/าห็ร้�บุการ้บุ�งค�บุ type ทั้/าได้�ค�อ เปี5น number : expr + 0

string : expr “”

Using awk : Action

Page 53: A Brief history

Array

เร้าสามาร้ถใชื่� array 1 ม7ตั7 ใน awk ได้� เขึ้$น

X[NR] = $0 โด้ยเร้าสามาร้ถ ใชื่� index เปี5น- numeric value- non numeric value

ตั�วอย$างเชื่$น /Asia/ {pop[“Asia”]

+= $3}

Using awk : Action

Page 54: A Brief history

Special feature

เร้าจะพั3ด้ถ�ง feature พั7เศษทั้%�เร้าสามาร้ถใชื่�ใน awk ได้�

- Bulit-In Function- Flow of control- Report generator- Cooperation with the shell- Multidimensional arrray

Using awk

Page 55: A Brief history

Built-In function

length การ้ห็าความยาวขึ้อง string เขึ้$น { print length,$0}

split ทั้/าการ้แยก field ไปีไว�ใน array โด้ยแบุ$ง filed ตัามทั้%�ก/าห็นด้ ถ�าไม$ก/าห็นด้จะใชื่� FS

split ( S,array,sep)substr ใชื่�ส/าห็ร้�บุตั�ด้ส$วนขึ้อง string เร้7�มตั�น และ ขึ้นาด้ตัาม ทั้%�ก/าห็นด้

substr (s , m , n)

Using awk : Special feature

Page 56: A Brief history

Index ทั้/าการ้ return ตั/าแห็น$งซึ่�ายส"ด้ทั้%� s2 ปีร้ากฏใน s1 ถ�า ไม$ปีร้ากฏจะ return 0

index (s1,s2)sprintfทั้/าการ้จ�ด้ร้3ปีแบุบุด้�วย printf statement แตั$จะให็�ค$า ไปีทั้%�ตั�วแปีร้

x = sprintf(“%10s %6d”,$1,$2)

Using awk : Special feature : Bulit-In function

Page 57: A Brief history

Flow of control

awk ม% control statement ให็�ใชื่�ด้�งน%�

- if ... else- while- for

Using awk : Special feature

Page 58: A Brief history

If … Else

ม%ร้3ปีแบุบุด้�งน%�if (condition)

statement1 else statement2ตั�วอย$าง

{ if (maxpop < $3) {maxpop = $3

country =$1 }

}END { print country , maxpop }

Using awk : Special feature : Flow of control

Page 59: A Brief history

While

ม%ร้3ปีแบุบุด้�งน%�while (condition)

statementตั�วอย$าง

{ i=1while (i <= NF) {

print $i++i

}}

Using awk : Special feature : Flow of control

Page 60: A Brief history

For

ม%ร้3ปีแบุบุด้�งน%�for (expr1,expr2,expr3)

statement statement2ตั�วอย$าง

{ for (i = 1;i <= NF;i++)print $i

}

Using awk : Special feature : Flow of control

Page 61: A Brief history

ม% statement อ��นทั้%�ใชื่�ก�บุ control statement ค�อ

break จะออกจาก loop ทั้�นทั้%continue จะกล�บุไปีทั้/างานทั้%�ตั�น

loopnext จะไปีทั้/างานทั้%� record

ถ�ด้ไปี และไปีทั้/างานทั้%� ตั�นขึ้องโปีร้แกร้ม

exit จะออกจากโปีร้แกร้มโด้ย ปีร้ากฏทั้%� BEGIN จะจบุการ้ทั้/า

งาทั้�นทั้% body จะไปีทั้/าทั้%� END END จะจบุการ้ทั้/างานทั้%�

จ"ด้น��น

Using awk : Special feature : Flow of control

Page 62: A Brief history

Report generator

พั7จาร้ณ์าจากตั�วอย$าง ด้�งน%� Sunti awk 9Ying nroff 8Nop make 7Dare dbx 5Ying troff 2Sunti awk 1Dare xdbx 5Nop mail 3

Using awk : Special feature

Page 63: A Brief history

Cooperation with the shell

พั7จาร้ณ์าโปีร้แกร้มชื่��อ filed ทั้%�บุร้ร้จ"ค/าส��งด้�งน%�

awk ‘{print $n}’โด้ยทั้%�เร้าส��ง execute field ด้�งน%�

field n เร้าม%ว7ธ%การ้ส$งค$า n ได้�สองว7ธ%ค�อ

awk ‘{print $’$1’}’และ awk “{print \$ $1}”

Using awk : Special feature

Page 64: A Brief history

Multidimensional array

เร้าสามาร้ถจ/าลองการ้ทั้/างานขึ้อง multidimensional array ได้�ด้�งน%�ค�อ

for ( i = 1 ; i <= 10 ; i++ ) for (j = 1 j <= 10 ; j++)

mult[i “,” j] = …

Using awk : Special feature

Page 65: A Brief history

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

Reference