Slippery Rock University | Dr. Deborah Whitfield | Go Browns! |
(see man awk!)
awk options 'selection_criteria {action}' file
awk [-F regexpr] -f progfile [-v asgn] [ arg ]
-F sets the field separator to the regular expression
-f Read the awk program from the progfile rather than the command line
-v Perform the awk assignment asgn before executing the awk program
Samples
awk '/browns/ { print }' foo
BEGIN { action } - perform the actions before processing the file
END {action} - perform the actions after processing
{ action } - perform actions to each input record
pattern [ {action} ] - perform action on only those records that match the pattern
pat1,pat2[{action}] - perform action in the range of records from pat1 to pat2
Rules for statements in an awk program:
Awk elements consist of constants (numbers and strings), variables, regular expressions, array elements, function calls, and parenthesized expressions.
Unlike the shell, mathematical operations do not need a call to expr.
User defined variables mimic the shell; however $0, $1, ... take on a different meaning.
Awk separates the entire input record into fields separated by whitespace. $1 is the first field, $2 is the second and $0 is the entire record.
The predefined variable FS. Assigning FS a different value (assignment statement or the -F flag), will cause awk to use that value as a separator.
Similarly, RS is the record separator and can be set to a single character. The default is \n.
Awk Operators
Control Structures
If statement is just like C's
if ( condition )
Stmt1
else
Stmt2
Built-In Variables (Section 12.10)
For statement is like C's NOT the shells
for (exp1; cond; exp2)
Stmt
While Statement (equivalent to above for):
exp1
while ( condition) {
stmt
exp2
}
awk Built-in functions (Section 12.11)
All work herein is subject to copyright. Original content to Dr. Deborah Whitfield, text content (Your UNIX/Linux) to Prentice Hall publishing.