BASIC AWK SYNTAX
awk [options] script file(s)
awk [options] f scriptfile file(s)
Options:
-F to change input field separator
-f to name script file
BASIC AWK PROGRAM
consists of patterns & actions:
pattern {action}
if
pattern is missing, action is applied to all lines
if action is missing, the matched line is printed
must have either pattern or action
Example:
awk '/for/' testfile
prints all lines containing string for in testfile
2
BASIC TERMINOLOGY: INPUT FILE
A field is a unit of data in a line
Each field is separated from the other fields by
the field separator
default
field separator is whitespace
A record is the collection of fields in a line
A data file is made up of records
EXAMPLE INPUT FILE
SOME SYSTEM VARIABLES
FS
Field separator (default=whitespace)
RS
Record separator (default=\n)
NF
Number of fields in current record
NR
Number of the current record
OFS
Output field separator (default=space)
ORS
Output record separator (default=\n)
FILENAME Current filename
EXAMPLE: RECORDS AND FIELDS
$ cat emps
Tom Jones
Mary Adams
Sally Chang
Billy Black
$
1
2
3
4
4424
5346
1654
1683
5/12/66
11/4/63
7/22/54
9/23/44
543354
28765
650000
336500
awk '{print NR, $0}' emps
Tom Jones
4424
5/12/66
Mary Adams
5346
11/4/63
Sally Chang
1654
7/22/54
Billy Black
1683
9/23/44
543354
28765
650000
336500
6
EXAMPLE: SPACE AS FIELD
SEPARATOR
$ cat emps
Tom Jones
Mary Adams
Sally Chang
Billy Black
$
1
2
3
4
4424
5346
1654
1683
5/12/66
11/4/63
7/22/54
9/23/44
543354
28765
650000
336500
awk '{print NR, $1, $2, $5}' emps
Tom Jones 543354
Mary Adams 28765
Sally Chang 650000
Billy Black 336500
7
EXAMPLE: COLON AS FIELD
SEPARATOR
$ cat em2
Tom Jones:4424:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500
$ awk -F: '/Jones/{print $1, $2}' em2
Tom Jones 4424
AWK SCRIPTS
awk scripts are divided into three major parts:
comment lines start with #
PATTERN / ACTION SYNTAX
10
CATEGORIES OF PATTERNS
11
EXPRESSION PATTERN TYPES
match
entire
input record
regular expression enclosed by /s
explicit
pattern-matching expressions
~ (match), !~ (not match)
expression operators
arithmetic
relational
logical
12
EXAMPLE: MATCH INPUT RECORD
% cat employees2
Tom Jones:4424:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500
% awk F: '/00$/' employees2
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500
13
EXAMPLE: EXPLICIT MATCH
% cat datafile
northwest NW
Charles Main
3.0
.98
34
western
Sharon Gray
5.3
.97
23
southwest SW
Lewis Dalsass
2.7
.8
18
southern
Suan Chin
5.1
.95
15
southeast SE
Patricia Hemenway
4.0
.7
17
eastern
TB Savage
4.4
.84
20
northeast NE
AM Main
5.1
.94
13
north
NO
Margot Weber
4.5
.89
central
CT
Ann Stephens
5.7
.94
13
WE
SO
EA
14
EXAMPLES: MATCHING WITH RES
% awk '$2 !~ /E/{print $1, $2}' datafile
northwest NW
southwest SW
southern SO
north NO
central CT
% awk '/^[ns]/{print $1}' datafile
northwest
southwest
southern
southeast
northeast
north
15
ARITHMETIC OPERATORS
Operator
Meaning
Add
Subtract
xy
Multiply
x*y
Divide
x/y
Modulus
x%y
Exponential x ^ y
Example
x+y
Example:
% awk '$3 * $4 > 500 {print $0}' file
16
RELATIONAL OPERATORS
Operator
Meaning
Example
<
Less than
x<y
<=
Less than or equal
x<=y
==
Equal to
x == y
!=
Not equal to
x != y
>
Greater than
x>y
>=
Greater than or equal to x > = y
Matched by reg expx ~ /y/
!~
Not matched by req exp
x !~ /y/
17
LOGICAL OPERATORS
Operator
Meaning
Example
&&
Logical AND
a && b
||
Logical OR
a || b
NOT
!a
Examples:
% awk '($2 > 5) && ($2 <= 15)
{print $0}' file
% awk '$3 == 100 || $4 > 50' file
18
RANGE PATTERNS
Matches ranges of consecutive input lines
Syntax:
pattern1 , pattern2 {action}
pattern can be any simple pattern
pattern1 turns action on
pattern2 turns action of
19
RANGE PATTERN EXAMPLE
20
AWK ACTIONS
21
AWK EXPRESSIONS
Expression is evaluated and returns value
consists of any combination of numeric and string
constants, variables, operators, functions, and
regular expressions
Can involve variables
As part of expression evaluation
As target of assignment
22
AWK VARIABLES
A user can define any number of variables within
an awk script
The variables can be numbers, strings, or arrays
Variable names start with a letter, followed by
letters, digits, and underscore
Variables come into existence the first time they
are referenced; therefore, they do not need to be
declared before use
All variables are initially created as strings and
initialized to a null string
23
AWK VARIABLES
Format:
variable = expression
Examples:
% awk '$1 ~ /Tom/
{wage = $3 * $4; print wage}'
filename
% awk '$4 == "CA"
{$4 = "California"; print $0}'
filename
24
AWK ASSIGNMENT OPERATORS
= assign result of right-hand-side expression to
left-hand-side variable
++
Add 1 to variable
--
Subtract 1 from variable
+=
Assign result of addition
-=
Assign result of subtraction
*=
Assign result of multiplication
/=
Assign result of division
%=
Assign result of modulo
^=
Assign result of exponentiation
25
AWK EXAMPLE
File: grades
john 85 92 78 94 88
andrea 89 90 75 90 86
jasper 84 88 80 92 84
awk script: average
# average five grades
{ total = $2 + $3 + $4 + $5 + $6
avg = total / 5
print $1, avg }
Run as:
awk f average grades
26
OUTPUT STATEMENTS
print
print easy and simple output
printf
print formatted (similar to C printf)
sprintf
format string (similar to C sprintf)
27
FUNCTION: PRINT
Writes to standard output
Output is terminated by ORS
default
ORS is newline
If called with no parameter, it will print $0
Printed parameters are separated by OFS,
default
OFS is blank
Print control characters are allowed:
\n
\f \a \t \\
28
PRINT EXAMPLE
% awk '{print}' grades
john 85 92 78 94 88
andrea 89 90 75 90 86
% awk '{print $0}' grades
john 85 92 78 94 88
andrea 89 90 75 90 86
% awk '{print($0)}' grades
john 85 92 78 94 88
andrea 89 90 75 90 86
29
PRINT EXAMPLE
% awk '{print $1, $2}' grades
john 85
andrea 89
% awk '{print $1 "," $2}' grades
john,85
andrea,89
30
PRINT EXAMPLE
% awk '{OFS="-";print $1 , $2}' grades
john-85
andrea-89
% awk '{OFS="-";print $1 "," $2}' grades
john,85
andrea,89
31
REDIRECTING PRINT OUTPUT
Print output goes to standard output
unless redirected via:
> file
>> file
| command
will open file or command only once
subsequent redirections append to already open
stream
32
PRINT EXAMPLE
% awk '{print $1 , $2 > "file"}' grades
% cat file
john 85
andrea 89
jasper 84
33
PRINT EXAMPLE
% awk '{print $1,$2 | "sort"}' grades
andrea 89
jasper 84
john 85
% awk '{print $1,$2 | "sort k 2"}' grades
jasper 84
john 85
andrea 89
34
PRINT EXAMPLE
% date
Wed Nov 19 [Link] CST 2008
% date |
awk '{print "Month: " $2 "\nYear: ",$6}'
Month: Nov
Year: 2008
35
ARRAYS IN AWK
Syntax:
arrayName[index] = value
Examples:
list[1] = "one"
list[2] = "three"
list["other"] = "oh my !"
36
ILLUSTRATION: ASSOCIATIVE ARRAYS
awk arrays can use string as index
37
AWK CONTROL STRUCTURES
Conditional
if-else
Repetition
for
with counter
with array index
while
do-while
also:
break, continue
38
IF STATEMENT
Syntax:
if (conditional expression)
statement-1
else
statement-2
Example:
if ( NR < 3 )
print $2
else
print $3
39
FOR LOOP
Syntax:
for (initialization; limit-test; update)
statement
Example:
for (i = 1; i <= NR; i++)
{
total += $i
count++
}
40
WHILE LOOP
Syntax:
while (logical expression)
statement
Example:
i = 1
while (i <= NF)
{
print i, $i
i++
}
41
DO-WHILE LOOP
Syntax:
do
statement
while (condition)
statement is executed at least once, even if
condition is false at the beginning
Example:
i = 1
do {
print $0
i++
} while (i <= 10)
42
LOOP CONTROL STATEMENTS
break
exits loop
continue
skips rest of current iteration, continues with
next iteration
43