AWK Scripting
AWK Scripting
CONTENTS
Introduction
Structure of awk Program
First awk Program
Searching For a String
Records and Fields
Awk Relational and Logical Operators
Formatted Output
BEGIN and END Statements
Awk Defined Variables
User Defined Variables
Regular Expressions
Match Operators
Concatenation Operator
Mohamed Mukthar Ahmed
Page 1
CONTENTS
Combining Patterns
Pattern Range
Awk Defined Variables Again
Built In Arithmetic Functions
Built In String Functions
Control Flow Statements
next Statement
Arrays in Awk
for Statement
User Defined Functions
Output to File
Output to Pipes
Awk Input
getline( ) Function
Mohamed Mukthar Ahmed
Introduction
Page 2
Introduction
Page 3
$ awk f myawk1
/etc/group
$ awk F: -f myawk1
/etc/passwd
Mohamed Mukthar Ahmed
/etc/passwd
| awk -f myawk2
Page 4
!=
>
>=
<
<=
~
!~
Not Equal to
Greater than
Greater than or equal to
Less than
Less than or equal to
Matches
Does not match
Logical Operators
Operator
Meaning
AND
&&
||
!
OR
NOT
Page 5
Formatted Output
# Formatted output
/mukthar/ { printf(UID = %d\tGID = %d\n,$3, $4) }
# myawk4
# Searching for a pattern mukthar
BEGIN { print Locating User mukthar }
/mukthar/ { printf(UID = %d\tGID = %d\n, $3, $4) }
/etc/passwd
Page 6
NF
# myawk5
# Number of valid users
END { print There are , NR , users }
$ awk F: -f myawk5
/etc/passwd
# myawk5b
# Counting users of training group
$4 == 505 { training++ }
END { print The number of users in ;
print training group are , training
}
$ awk F: -f myawk5b
/etc/passwd
Mohamed Mukthar Ahmed
Page 7
Regular Expressions
# myawk6
# Searching for a user1 to user5
BEGIN { print Locating User1 to User5 }
/^user[1-5]/ { print $1, $3, $4 }
END { print End of Report }
$ awk F: -f myawk6
/etc/passwd
Regular Expressions
# myawk7
# Searching for UIDs 501-505
BEGIN { print Locating UIDs 501-505 }
$3 ~ /50[1-5]/ { print $1, $3, $4 }
END { print End of Report }
Pattern
Meaning
. DOT any one character
\
[ ]
[^
[^]
^
$
despecialize character
any one character in the list. Character Class
any one character not in the list
beginning of line
Also called as anchors
end of line
Mohamed Mukthar Ahmed
Page 8
Regular Expressions
{n,}
{n,m}
n,m}
Concatenation Operator
The plus (+
(+) symbol concatenates one or more
strings in pattern matching.
# myawk8
# Searching for a pattern $unix
BEGIN { print Locating $Unix or $unix }
$1 ~ /\$+[Uu]nix/ { print $0 }
END { print End of Report }
Page 9
Combining Patterns
Patterns can be combined to provide more
powerful and complex matching.
# myawk9
# Combineing patterns
BEGIN { print Combining Patterns }
$1 == 486 && $5 > 250 { print $0 }
END { print End of Report }
# myawk9b
/user1/,/user8/ { print $0 }
Mohamed Mukthar Ahmed
Variable
NR
NF
FS
FILENAME
FNR
OFS
ORS
ARGC
ARGV
PrePre-defined Variables
Meaning
The current input line.
Number of fields in the input line.
Input field separator.
Name of current input file.
Record number in current input file.
Output field separator.
Output record separator.
Number of command line arguments.
Array of command line arguments.
Mohamed Mukthar Ahmed
Page 10
Examples
Examples on using prepre-defined variables of awk.
awk.
# myawk10
# Print the first five input lines.
FNR == 1, FNR == 5 { print $0 }
# myawk11
# Print each input line with line number.
# Print the heading with file name.
BEGIN { print "File :", FILENAME }
{ print NR, ":\t", $0 }
# myawk12
BEGIN { print "There are ", ARGC, "parameters on the
command line";
print "The first argument is ", ARGV[0];
print "The second argument is ", ARGV[1];
}
Mohamed Mukthar Ahmed
int(x)
int(x)
sqrt(x)
sqrt(x)
rand(x)
rand(x)
srand(x)
srand(x)
exp(x)
exp(x)
log(x)
log(x)
sin(x)
sin(x)
cos(x)
cos(x)
Arithmetic Functions
Description
Integer part of x
Square root of x
Random number between 0 and 1
x is a new seed for rand( )
Exponential function of x
Natural Logarithm of x
Sine of x, with x in radians
Cosine of x, with x in radians
Mohamed Mukthar Ahmed
Page 11
Examples
Examples on builtbuilt-in functions of awk.
awk.
# myawk13
# Print the square root of input value
{ print sqrt( $1 ) }
$ awk f myawk13
2
1.41421
3
1.73205
4
2
String Functions
Description
Return length of s
length(s)
length(s)
Returns substring of s from position p
substr(s,p)
substr(s,p)
substr(s,p,n)
substr(s,p,n) Returns substring of s from position p of
index(s,t)
index(s,t)
match(s,t)
match(s,t)
split(s,a)
split(s,a)
split(s,a,fs)
split(s,a,fs)
length n
Returns position of t in string s
Returns position of t in string s
Page 12
gsub(r,s)
gsub(r,s)
gsub(r,s,t)
gsub(r,s,t)
String Functions
Description
Substitutes s in place of r in $0 globally.
Returns the number of substitutions made
Substitutes s in place of r in t. Returns the
number of substitutions made.
Substitutes s for first r.
sub(r,s)
sub(r,s)
Substitutes s for first r in t.
sub(r,s,t)
sub(r,s,t)
sprintf(fmt Returns expression list formatted according
to format string specified by fmt.
fmt.
,expr,expr-lst)
lst)
# myawk15
# Finding biggest disk
{
if (disksize < $5 )
{
disksize = $5;
computer = $0
}
}
END
{ print
computercomp_data
}
$ awk
-f myawk15
Mohamed Mukthar Ahmed
Page 13
BEGIN { printf("Type\tLoc\tDisk\n"); }
/286/ { field = 1;
while( field <= NF )
{
printf("%s\t", $field);
field += 2;
}
print "";
}
$ awk
-f myawk16
comp_data
Mohamed Mukthar Ahmed
# myawk17
# Print out computer type 286 using next
{
while($1 != 286)
next;
print $0
}
$ awk
-f myawk17
comp_data
Mohamed Mukthar Ahmed
Page 14
Arrays in AWK
# Array Examples
ARRAY[e001] = 100
print ARRAY[e001]
ARRAY[
ARRAY[num
num] and ARRAY[num] are not the same.
Arrays - Examples
Examples
# myawk18
$1 == "486" { computers["486"]++ }
$5 > 0
{ diskspace[0] += $5 }
END {
print "Number of 486 computers :" ,
computers[486];
print "Total disk space :",
diskspace[0];
}
$ awk
-f myawk18
comp_data
Mohamed Mukthar Ahmed
Page 15
Arrays in AWK
# Array Examples
if (e001 in ARRAY)
delete ARRAY[e001]
for Statement
Syntax:
for ( var in array ) statement(s)
The var get one element at a time from the array and
executes the statement until the array elements are
not exhausted.
Page 16
for - Examples
Examples
# myawk19
# Counting number of computers of all types
{ computers[$1]++ }
END {
for(name in computers)
print Number of , name, computers
is , computers[name]
}
$ awk
-f myawk19
comp_data
Syntax:
function name(arg_list)
{
statements
}
Page 17
UDF - Examples
Examples
# myawk20
# Finding factorial
function factorial(n) {
if(n<=1) return 1
else return n*factorial(n-1)
} #End of function
{
print Factorial of,$1,is,factorial($1)
}
$ awk
-f myawk20
Output To File
# myawk21
# Output to file
$1 == "486" {
print "Type = ", $1 , "Location = ", $3
}
$ awk
> "comp486.dat"
-f myawk21 comp_data
Page 18
# myawk22
# Using getline function
if ($1 == "486) {
FIRSTLINE = $0;
getline;
SECONDLINE = $0
}
$ awk
-f myawk22 comp_data
Mohamed Mukthar Ahmed
Moreover,
# myawk23
{
print NR, $0
getline;
print NR, $0
}
$ echo 100 200 300 400 500 600 | awk
-f myawk23
Page 19
getline var
FNR
# myawk24
# Using getline function
while( getline < data )
print $0;
$ awk
-f myawk24
getline var < file Read next line from file into var,
var,
increment NR FNR
cmd | getline var Read next line from cmd into var
# myawk25
# Using getline function
while( who | getline )
print user , $1, tty, $3;
$ awk
-f myawk25
Mohamed Mukthar Ahmed
Page 20