% Sed - N - e 1,50p' Datafile % Head - 50 Datafile: Linux Programming
% Sed - N - e 1,50p' Datafile % Head - 50 Datafile: Linux Programming
AWK
WHAT IS AWK?
created by: Aho, Weinberger, and Kernighan
scripting language used for manipulating data and generating reports
versions of awk
awk, nawk, mawk, pgawk, …
GNU awk: gawk
What can you do with awk?
awk operation:
scans a file line by line
splits each input line into fields
compares input line/fields to pattern
performs action(s) on matched lines
Useful for:
transform data files
produce formatted reports
Programming constructs:
format output lines
arithmetic and string operations
conditionals and loops
The Command:
awk
Buffers
AWK SCRIPTS
awk scripts are divided into three major parts:
Categories of Patterns
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
Example: explicit match
% cat datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
% awk '$5 ~ /\.[7-9]+/' datafile
southwest SW Lewis Dalsass 2.7 .8 2 18
central CT Ann Stephens 5.7 .94 5 13
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
ARITHMETIC OPERATORS
Operator Meaning Example
+ Add x+y
- Subtract x–y
* Multiply x*y
/ Divide x/y
% Modulus x%y
^ Exponential x^y
Example:
% awk '$3 * $4 > 500 {print $0}' file
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 exp x ~ /y/
!~ Not matched by req exp x !~ /y/
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
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 off
Range Pattern Example
AWK ACTIONS
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
awk variables
jasper 84
john 85
% awk '{print $1,$2 | "sort –k 2"}' grades
jasper 84
john 85
andrea 89
% date
Wed Nov 19 14:40:07 CST 2008
% date |
awk '{print "Month: " $2 "\nYear: ", $6}'
Month: Nov
Year: 2008
printf: Formatting output
Syntax:
printf(format-string, var1, var2, …)
works like C printf
each format specifier in “format-string” requires argument of matching type
Format specifiers
%d, %i decimal integer
%c single character
%s string of characters
%f floating point number
%o octal number
%x hexadecimal number
%e scientific floating point notation
%% the letter “%”
Format specifier examples
Given: x = ‘A’, y = 15, z = 2.3, and $1 = Bob Smith
print "======================================"
}
{
printf("%3d\t%-20s\t%6.2f\n", $1, $2, $3)
count++
}
END {
print "======================================"
print "Catalog has " count " parts"
}
awk Array
awk allows one-dimensional arrays
to store strings or numbers
index can be number or string
array need not be declared
its size
its elements
array elements are created when first used
initialized to 0 or “”
Arrays in awk
Syntax:
arrayName[index] = value
Examples:
list[1] = "one"
list[2] = "three"
list["other"] = "oh my !"
Illustration: Associative Arrays
awk arrays can use string as index
output:
summary of category sales
Illustration: process each input line
deptSales[$2] += $3
}
END {
for (x in deptSales)
print x, deptSales[x]
}
% awk –f sales.awk sales
Awk control structures
Conditional
if-else
Repetition
for
with counter
with array index
while
do-while
also: break, continue
if Statement
Syntax:
if (conditional expression)
statement-1
else
statement-2
Example:
if ( NR < 3 )
print $2
else
print $3
for Loop
Syntax:
for (initialization; limit-test; update)
statement
Example:
for (i = 1; i <= NR; i++)
{
total += $i
count++
}
for Loop for arrays
Syntax: