1 / 65

A Brief history

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

huy
Download Presentation

A Brief history

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction to Awk • awk คือภาษาโปรแกรมสำหรับประมวลผลแฟ้มข้อมูลที่ถูกออกแบบมาสำหรับการสร้างข้อมูลทั่วไปและใช้กับงานที่ใช้ในการดึงข้อความ ซึ่งง่ายสำหรับการใช้และการทำงาน โดย awk สามารถทำดังนี้คือ • ทำรายงาน • การทำ pattern matching • การตรวจสอบข้อมูล • กรองข้อมูล

  3. Program Structure awk โปรแกรมจะมีลักษณะเป็นชุดคำสั่ง ดังนี้ Pattern {Action} Pattern {Action} . . . - awk จะทำงานกับชุดข้อมูลของแฟ้มข้อมูลเข้า โดยทำงานใน ในรูปแบบ record และ field - เราสามารถละการเขียน pattern และ action ได้

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

  5. Lexical Units Numeric constants เป็นชุดของตัวอักขระที่เป็นตัวเลข มีค่าเป็นจำนวนเต็มและทศนิยม โดยทศนิยมเขียนได้ดังนี้คือ เช่น 1.2 0.12 หรือ 1.2e2 String constants เป็นชุดของตัวอักขระที่เป็นตัวอักษร และ ตัวเลข มีค่าเป็น ล้อมรอบด้วย “ “ ถ้าต้องการพิมพ์ “ ให้ใช้ \“ เช่น “hello”

  6. Lexical Units

  7. Lexical Units Identifier เป็นชุดของตัวอักขระที่เป็นตัวอักษร ตัวเลขและเครื่องหมายขีดเส้นใต้ (underscore) ใช้สำหรับเป็นตัวแปร หรือ array โดยชื่อต้องขึ้นต้นด้วยตัวอักษรหรือ _ เช่น var1 หรือ _var1 ตัวอักษรตัวใหญ่และตัวเล็กความหมายไม่เหมือนกัน เช่น Var1 และ var1

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

  9. Lexical Units Assignment operators

  10. Lexical Units Arithmetic operators

  11. Lexical Units Relational operators

  12. Lexical Units Logical operators Regular expression matching operators

  13. 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 ขณะนั้น

  14. Lexical Units Comment การเขียน comment จะใช้เครื่องหมาย # นำหน้าส่วนที่จะ comment ไปจนจบบรรทัด Token used for grouping แต่ละ token ใน awk จะถูกแบ่งด้วย blank , tabs หรือ newline หรือเครื่องหมายอื่น เช่น { … } ใช้ล้อมรอบ Action / … / ใช้ล้อมรอบ Pattern “ … “ ใช้ล้อมรอบ String

  15. Primary expression ใน action และ pattern ของ awk ถูกสร้างมาจาก block ของ expression ซึ่งประกอบขึ้นด้วย expression ต่อไปนี้ - Numeric constants - String constant - variables - Function โดย expression ข้างต้นสามารถให้ค่า ได้ทั้งตัวเลข และ string อย่างใดอย่างหนึ่ง

  16. Primary expression Numeric Constant ตารางตัวอย่างค่าของค่าคงที่

  17. Primary expression String Constant ตารางตัวอย่างค่าของค่าคงที่

  18. Primary expression Variables มีรูปแบบดังนี้ identifiers ตัวแปรธรรมดา identifier[expression]ตัวแปร array $Termตัวแปรอ้างถึง record ข้อมูล

  19. Primary expression : variables $Term - Term เป็นค่าตัวเลขที่มีค่ามากกว่า 0 อ้างถึง field ที่ i - ให้ i เป็นค่ามากที่สุดของ term ถ้า i < 0 และ i > 100 awk จะตรวจสอบ error ถ้า NF < i < 100 $i จะเป็นตัวแปรที่ยังไม่กำหนดค่า - การเข้าถึง $i ที่ i > NF จะไม่ทำให้ค่า NF เปลี่ยน

  20. Primary expression Function awk มี function ให้ใช้ คือ arithmetic และ String Function

  21. Primary expression

  22. Term สำหรับการทำงานทางคณิตศาสตร์ จะนำเอา expression มาทำการสร้างกลุ่มของ ไวยากรณ์ที่มีขนาดใหญ่เรียกว่า term โดยมีรูปแบบดังนี้ Primary expression term binop term unop term incremented variable (term)

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

  24. 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

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

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

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

  28. Using awk Input and output เราสามรถเขียน awk programได้สองวิธีคือ 1. Command line มีรูปแบบดังนี้ awk ‘program’ file1 โดยที่จะรับ program มาเป็น argument แรกของ awk โดยที่เราใช้ ‘ ‘ เพื่อให้แน่ใจว่า program จะเป็น argument แรกของ awk ตัวอย่างเช่น awk ‘/x/ {print}’ file1

  29. Using awk : Input and output 2. เราสามารถเขียน awk program ไว้ใน file ได้เมื่อ program ของเรามีขนาดยาว และเรายังสามารถนำ เอากลับมาใช้ใหม่ได้ เช่น เก็บไว้ใน awkprog เรา เรียกโดยการใช้ option -f โดยสามารถเรียกใช้ดังนี้ awk -f awkprog file1

  30. 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 ได้

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

  32. 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 … ตามลำดับ

  33. Using awk : Input and output : Input Input : from command line ใน awk program เราสามารถประกาศตัวแปรได้ทันที เช่น x = 5 และเขียนเป็นคำสั่ง awk ‘{print x}’ x=5 - เป็นการกำหนดค่า x โดยให้ x=5 และรับ input จาก standard input โดยใช้เครื่องหมาย - แทนการรับจาก file ข้อมูล

  34. 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

  35. Using awk : Input and output : Output Output : Printing จากแฟ้มข้อมูลตัวอย่าง เราสามารถพิมพ์ ค่าออกมาโดยใช้ program ต่อไปนี้ awk ‘{print $1,$2 ,$3}’ countries ส่วน semicolon เราจะใช้ในการแบ่งคำสั่งดังนี้ {x = 5 ; print x } ( ... ) ใช้ในการจัดกลุ่ม เช่น {print ($1,$2)}

  36. Using awk : Input and output : Output เราสามารถใช้ตัวแปรพิเศษในการพิมพ์ output ออกมาได้เช่น NR เก็บค่าจำนวนชอง record ขณะนั้น NF เก็บค่า จำนวนของ fileds ใน record นั้น ตัวอย่างการใช้งานเช่น { print NR , NF , $0 }

  37. Using awk : Input and output : Output และ OFS เก็บอักขระที่ใช้ในการแบ่ง field ที่เป็นผลลัพธ์ ปกติจะมีค่าเป็น blank ORS เก็บอักขระที่ใช้ในการแบ่ง record ที่เป็นผล ลัพธ์ ปกติจะเป็น newline charecter ตัวอย่างการใช้งานเช่น { x= “hello” ; y=“, world” print x ,y } ผลที่ได้คือ hello, world

  38. Using awk : Input and output : Output Output : to Different file awk อนุญาตให้มีการส่งผลลัพธ์ไปยัง file ได้โดยสามารถเขียนคำสั่งดังนี้ print > “filename” โดยสามารถทำการเปิดได้สูงสุด 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” }

  39. 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” }

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

  41. Using awk : Pattern BEGIN and END BEGIN และ END เป็น pattern พิเศษโดยที่ BEGION : จะทำการ match ส่วนเริ่มต้นของ input ก่อนที่ record แรกจะถูกอ่าน END : จะทำการ match ส่วนสุดท้ายของ input หลังจาก record สุดท้ายถูกอ่าน ตัวอย่างการใช้ คือ BEGIN {print “Country” , “Area” , “Population” , “Continent” } { print }

  42. Using awk : Pattern Relational expression เราใช้ expression ในการเปรียบเทียบในการสร้าง pattern เช่น $3 > 1000 หรือ $4 == “Asia” {print $1} Operator สำหรับการเปรียบเทียบคือ < , <= , == ,>= ,> , !

  43. Using awk : Pattern 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

  44. Using awk : Pattern Combination of these เราสามารถนำเอา pattern เหล่านี้มาเขียนรวมกันได้โดยใช้ operators && (AND) , || (OR) หรือ ! (NOT) เช่น $2 > 3000 && $3 > 100 $4 == “Asia” || $4 == “Africa” $4 ~ /^(Asia|Africa)$/ ซึ่งสอง pattern สุดท้ายได้ผลลัพธ์เท่ากัน

  45. Using awk : Pattern Pattern range pattern ที่อยู่ในรูปแบบ pattern1 , pattern2 {action} awk จะทำงานโดยทำกับ record ที่อยู่ระหว่าง บรรทัดที่เริ่มที่บรรทัดที่มีค่าปรากฏใน pattern1 จนการกระทั้งถึง บรรทัดที่มีค่าปรากฏใน pattern2 เข่น /Canada/ , /Brazil/ NR == 2 , NR == 5

  46. Using awk Action action เป็นชุดของคำสั่งที่แต่ละคำสั่งจะแยกด้วย การขึ้นบรรทัดใหม่ หรือ การใช้ semicolon (;) Variables , Expression and Assignment awk สนับสนุนการทำงานทางคณิตศาสตร์และเก็บค่าลงในตัวแปรเพื่อนำเอามาใช้ในภายหลัง เช่น /Asia/ { pop += $3 ; ++ n } END { print “total population of ”, n , “Asian countries is “ , pop }

  47. Using awk : Action Initialized of variables ในตัวอย่างก่อนหน้านี้เราไม่ได้ทำการให้ค่าเริ่มต้นกับตัวแปร pop และ n แต่โปรแกรมยังทำงานถูกเนื่องมาจาก ถ้าเป็นชนิดตัวเลข จะถูกตั้งค่าเป็น 0 ถ้าเป็นชนิด string จะถูกตั้งต่าเป็น null string ดังนั้นเราสามารถให้ค่าที่กำหนดไว้แล้วเขียน program ได้ดังนี้ maxpop < $3 { maxpop = $3 country = $1 } END { print country , maxpop }

  48. Using awk : Action Field variable เราสามารถให้ค่ากับตัวแปรที่เป็น fields ได้ดังนี้เช่น { $2 /= 1000; print } หรือ BEGIN { FS = “\t” } { $4 = 1000 * $3 / $2 ; print } หรือ /USA/ { $1 = “United State” ; print }

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

More Related