linux crash course chapter 12 3

24 71 0
 linux crash course chapter 12 3

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 12: gawk Yes it sounds funny In this chapter … • • • • • Intro Patterns Actions Control Structures Putting it all together gawk? • • • • GNU awk awk == Aho, Weinberger and Kernighan Pattern processing language Filters data and generates reports gawk con’t • Syntax: gawk [options] [program] [file-list] gawk [options] –f program-file [file-list] • Essentially, program is a list of things to pattern match, and then a list of actions to perform • Can either be on the command line or in a file gawk program • A gawk program contains one or more lines in the format pattern { action } • Pattern is used to determine which lines of data to select • Action determines what to with those lines • Default pattern is all lines • Default action is to print the line • Use single quotes around program on CL Patterns • Simple numeric or string comparisons < = > • Regular expressions (see Appendix A) – The ~ operator matches pattern – The !~ operator does not match pattern • Combinations using || (OR) and && (AND) Patterns, con’t • BEGIN – before any lines are processed • END – after all lines are processed • pattern1,pattern2 – a range, that starts with pattern 1, and ends with pattern2 After matching pattern2, gawk attempts to match pattern1 again Variables • $0 – the current record (line) • $1-$n – fields in current record • FS – input field separator (default: SPACE / TAB) • NF – number of fields in record • NR – current record number • RS – input record separator (default: NEWLINE) • OFS – output field separator • ORS – output record separator Associative Arrays • A variable type similar to an array, but with strings as indexes (instead of integers) • Ex – myAssocArray[name] = “Bob” – myAssocArray[hometown] = “Austin” • Ex – studentGrades[123-45-6789] = 75 – studentGrades[987-65-4321] = 100 Pattern examples • $1 ~ /^[A-Z]/ – Matches records where first field starts with a capital letter • $3 5000 && $1 !~ /exempt/ – Matches records where second field is greater than 5000 and first field is not exempt Functions • length(str) – returns length of str – Returns length of line if str omitted • int(num) – returns integer portion of num • tolower(str) – coverts chars to lower case • toupper(str) – converts chars to upper case • substr(str,pos,len) – returns substring of str starting at pos with length len Actions • Default action is print entire record • Using print, can print out particular parts (i.e., fields) – Ex { print $1 } • Put literal strings in single quotes • By default multiple parameters catenated – Use comma to use OFS • Ex { print $1, $5 } Actions, con’t • Separate multiple actions by semicolons • Other actions usually involve variables (i.e., incrementors, accumulators) • Variables need not be formally initialized • By default set to zero or null • Standard operators function normally * / % + - = ++ += -= *= /= %= Actions, con’t • Instead of print you can use printf (c-style) • Syntax: – printf “control-string”, arg1, arg2 … argn contains one or more conversion – %[-][[x].[y]]conv – control-string • - – left justify x – field width y – decimal places • conv: d – decimal f – floating point s – string • Ex: %.2f – floating point with two decimal places Control Structures • gawk programs can utilize several control structures • Can use if-else, while, for, break and continue • All are C-style in syntax (what did the K in gawk stand for?) if … else • Syntax: if (condition) { commands } else { commands } while • Syntax: while (condition) { commands } for • Syntax: for (init; condition; increment) { commands } • You can use break and continue for both for and while loops Examples • • • • • • • • • gawk gawk gawk gawk gawk gawk gawk gawk gawk ‘{print}’ cars ‘/chevy/’ cars ‘{print $3, $1}’ cars ‘/chevy/ {print $3, $1} cars ‘$1 ~ /^h/’ cars ‘2000

Ngày đăng: 06/02/2018, 09:55

Mục lục

  • Chapter 12: gawk

  • In this chapter …

  • gawk?

  • gawk con’t

  • gawk program

  • Patterns

  • Patterns, con’t

  • Variables

  • Associative Arrays

  • Pattern examples

  • Functions

  • Actions

  • Actions, con’t

  • Slide 14

  • Control Structures

  • if … else

  • while

  • for

  • Examples

  • Putting it all together

Tài liệu cùng người dùng

Tài liệu liên quan