Presentation is loading. Please wait.

Presentation is loading. Please wait.

Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology

Similar presentations


Presentation on theme: "Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology"— Presentation transcript:

1 awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

2 Tran, Van Hoai 2010 awk What is " awk "? awk = programming language  text/string manipulation within shell script  useful for input data in records/fields

3 Tran, Van Hoai 2010 awk Typical uses Perform arithmetic and string operations Use loops and conditionals Produce formatted report Process UNIX command arguments Execute UNIX commands from script …

4 Tran, Van Hoai 2010 awk Examples (1) How to print the first three fields of each record on separate lines Can we do by shell script and " cut " ? We can do by " awk " in one command awk -F: '{ print $1; print $2; print $3 }' /etc/passwd

5 Tran, Van Hoai 2010 awk Syntax Command forms awk [options] 'script' var=value file(s) awk [options] -f scriptfile var=value file(s) Variable assignment on command-line " value " can be  string, numeric constant  shell variable ( $name )  command substitution ( `cmd` )

6 Tran, Van Hoai 2010 awk Options Standard options OptionsMeaning -Ffs Set the field separator to fs. -v var=value Assign var to value, performed before script execution Advanced options (see man pages)

7 Tran, Van Hoai 2010 awk Patterns and procedures Script is pattern { action } Both are optional  pattern missing, apply to all lines  action missing, matched line is printed

8 Tran, Van Hoai 2010 awk Patterns general expression composed of quoted strings, numbers, operators, function calls, used-defined/pre-defined variables /regular expression/ ^ and $ are for beginning and end of strings (fields) (not line) relational expression e.g., comparison like $2>$1

9 Tran, Van Hoai 2010 awk Patterns BEGIN actions performed before first input line processed END actions performed after last input line processed combination of patterns " || " (or), " && " (and), " ! " (not), ", " (range)

10 Tran, Van Hoai 2010 awk Procedures Separated by newline, semicolons Contained in " {} " Command groups Variables, array assignments Input/output commands Built-in functions Control-flow commands User-defined functions

11 Tran, Van Hoai 2010 awk Example (2) Print first field of each line { print $1 } Print all lines containing pattern /pattern/ Print first field of lines containing pattern /pattern/ { print $1 } Select record having > 2 fields NF > 2

12 Tran, Van Hoai 2010 awk Example (3) Print only lines which first field matches URGENT $1 ~ /URGENT/ { print $3, $2 } Print number of lines matching pattern /pattern/ {x++} END {print x} Sum up column 2 and print the total {total += $2} END { print total}

13 Tran, Van Hoai 2010 awk Example (4) Print lines containing < 20 characters length($0) < 20 Print lines of 7 fields and beginning with " Name: " NF==7 && /^Name:/

14 Tran, Van Hoai 2010 awk Example (5) Print fields in reverse order one per line { for (i=NF; i>=1; i--) print $i; }

15 Tran, Van Hoai 2010 awk Built-in variables FS Field separator NR Number of current record NF Number of fields in current record RS Record separator $0 Entire input record $n n-th field in current record FILENAME Current filename

16 Tran, Van Hoai 2010 awk Operators =,+=,*=,… Assignments ?: C conditional ||, && or, and in Array membership,>=,==,!= Relational operators +,-,*,… Arithematic operators $ Field reference

17 Tran, Van Hoai 2010 awk awk functions and commands Control flow  break, continue, do/while, exit, for, if/else, return, while Arithmetic  rand, sin, cos,… String IO  print, printf, getline,…

18 Tran, Van Hoai 2010 awk Exercises is NEXT


Download ppt "Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology"

Similar presentations


Ads by Google