Professional Documents
Culture Documents
com/awk/2
awk Commands, Examples & Meaning
awk Examples
1. Extract the used space ( fifth field in df output ) column by mount points using df output
Similar operation to extract 1st and 4tgh column from a file called testfile containing following
lines
column1 column2 column3 column4
1111 2222 3333 4444
1111 2222 3333 4444
1111 2222 3333 4444
Comma separating fields gives a default space between the output data fields. For large number
of fields a special awk variable, Output Field Separator, OFS is used . Default is a space and it
can be assign to any other value , such as a pipe symbol , | , in the example below.
Example:
awk programs can be written and invoked from a file by providing awk interpreter location in
the first liner ,
Syntax :
$awk -f <program file> <datafile>
Create a awk program test file, chkrec as below.
#! /bin/awk -f
BEGIN { print “Count Records ” }
/4444/ { ++num }
END { print “Recs ” num }
Execute file with -f option
localhost ~]$ awk -f chkrec testfile
Count Records
Recs 3
or make it executable & directly execute with data file as argument
localhost ~]$ chmod 755 chkrec
localhost ~]$ ./chkrec testfile
Count Records
Recs 3
1. Compare values
print Available Use% Mounted columns if used percentage is more than 60%
Add file sizes for selective files, /var/log/yum* and total sum is printed , column from each line
is added in variable n and total is printed with END statement.
Check available space , print ok in front of the output if less than 60% and Problem if more than
60%
Print 1 to 5 numbers using a for loop by proving initial value , final value and increment
function.
A[“ZZ”] = “Last”
A[“DD”] = “Middle”
A[“AA”] = “First”
Sorting arrays