awk Commands, Examples & Meaning

awk Examples

1. Extract the used space ( fifth field in df output ) column by mount points using df output

localhost ~]$ df | awk ‘{print $3 }’
Used
10831280
0
252
1020
0
176
123767
51118256
66006

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

localhost ~]$ awk ‘{print $1,$4}’ testfile
column1 column4
1111 4444
1111 4444
1111 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.

localhost ~]$ awk ‘{OFS=”|” ; print $3,$4}’ testfile
column3|column4
3333|4444
3333|4444
3333|4444

awk BEGIN and END statement

Multi-line program uses BEGIN and END statements to execute statements once at the beginging and at the end.
basic construction is  :
BEGIN <statment>
<processing statments >
END <statment>

Example:

localhost ~]$ awk ‘BEGIN { print “Count Records ” }
/4444/ { ++num }
END { print “Recs ” num }’ testfile
Count Records
Recs 3

awk program File

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

Awk Example programs

1. Compare values

print Available Use% Mounted columns if used percentage is more than 60%

localhost ~]$ df| awk ‘$5 > “60” { print $4,$5,$6}’
Available Use% Mounted
4522188 92% /home
32298 68% /boot/efi

2.awk Sum operations

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.

localhost ~]$ ls -l /var/log/yum* | awk ‘{ n += $5 }
END { print “Total bytes = “, n }’
Total bytes = 63665

3. awk if else conditions

Check available space , print ok in front of the output  if less than 60% and Problem if more than 60%

$df | awk ‘{ if ($5 > 60) print “Problem “$0
else
print “ok “, $0
};’
Problem Filesystem 1K-blocks Used Available Use% Mounted on
ok /dev/mapper/fedora-root 51475068 10831316 38005928 23% /
ok devtmpfs 1956180 0 1956180 0% /dev
ok tmpfs 1966388 252 1966136 1% /dev/shm
ok tmpfs 1966388 992 1965396 1% /run
ok tmpfs 1966388 0 1966388 0% /sys/fs/cgroup
ok tmpfs 1966388 176 1966212 1% /tmp
ok /dev/sda9 487652 123767 334189 28% /boot
Problem /dev/mapper/fedora-home 58642620 51118476 4522188 92% /home
Problem /dev/sda2 98304 66006 32298 68% /boot/efi

4.awk for loop

Print 1 to 5 numbers using a for loop by proving initial value , final value and increment function.

localhost ~]$ awk ‘BEGIN { for (i = 1; i <= 5; ++i) print i }’
1
2
3
4
5

5. awk Arrays , creating and sorting

Create a  array by assigning values to array indexes :

A[“ZZ”] = “Last”
A[“DD”] = “Middle”
A[“AA”] = “First”

Sorting arrays

asorti – Array Sort by Indices
asort – Array Sort by value
asort(A)
A[“AA”] = “First”
A[“ZZ”] = “Last”
A[“DD”] = “Middle”
asorti – Array Sort by Indices
asprti(A)
A[“AA”] = “First”
A[“DD”] = “Middle”
A[“ZZ”] = “Last”

Next Page – awk regular expressions

1 thought on “awk Commands, Examples & Meaning

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.