Exercise #5: Managing the Structure of the awk file

by mike on February 16, 2011

The awk file can have three parts which allow you to perform various tasks and enhance your script and it’s usability.  The three sections are; BEGIN, BODY and END.  The BEGIN section must have the “BEGIN” text to indicate that it will be used.  The BODY actually does not need any reference so that anything you place in the BODY section will execute just as if it was on the command line.  A script also allows you to run many different tasks throughout the script.  The END section must begin with the term “END”.

Here is the basic awk file called records:

BEGIN {print "Start Evaluating records"}
NR > 12 {print "Number of Users is Now More Than 12"}
END {print "Total Number of Users:  " NR}

The awk file has three sections:
BEGIN
BEGIN {print "Start Evaluating records"}
The BEGIN section simply is printing  a message to the screen.  You can perform many different tasks in this section.
BODY
NR > 12 {print "Number of Users is Now More Than 12"}
The BODY is using an operator “>” to say that if the number of records “NR” is greater than 12, then print the text string.  This will print each time a record is evaluated.
END
END {print "Total Number of Users:  " NR}
The END section is used to print the total number of records “NR”.

awk -f records /etc/passwd
Start Evaluating records
Number of Users is Now More Than 12
Number of Users is Now More Than 12
---cut---
Number of Users is Now More Than 12
Number of Users is Now More Than 12
Total Number of Users:  32

awk file: header
In this example the goal is to create a new header for the output while selecting only some fields to print.  The BEGIN section has the header inside quotes followed by the body which prints 5 fields.

BEGIN {print "Owner     PID     CPU     MEM     Process"}
{print $1,$2,$3,$4,$11}

Obviously there are several problems, one is format.  The header is separated by tabs but the output is not.

awk -f header processes
Owner    PID    CPU    MEM    Process
USER PID %CPU %MEM COMMAND
root 1 0.0 0.2 init
root 13638 0.0 0.2 /sbin/udevd
root 13880 0.0 0.2 syslogd
root 13901 0.0 0.4 /usr/sbin/sshd
root 13910 0.0 0.3 xinetd
root 13977 0.0 0.6 sendmail:
root 14022 0.0 1.0 /usr/sbin/httpd
apache 14023 0.0 0.7 /usr/sbin/httpd
root 14031 0.0 0.4 crond
root 14039 0.0 0.2 /usr/sbin/saslauthd
root 14040 0.0 0.1 /usr/sbin/saslauthd
root 15857 0.0 1.1 sshd:
root 15864 0.0 0.5 -bash
root 15969 0.0 0.3 ps

awk file: header
There are several modifications to the header file.  First, “OFS”, the output field separator is changed to a tab, that lines up all of the output.  Second, in the body a search is made for records that have “httpd” and then five fields are printed.

BEGIN {OFS= "\t"
print "Owner    PID     CPU     MEM     Process"}
/httpd/ {print $1,$2,$3,$4,$11}

awk -f header processes
Owner     PID     CPU     MEM    Process
root     14022    0.0     1.0    /usr/sbin/httpd
apache   14023    0.0     0.7    /usr/sbin/httpd

awk file: header
Add an additional search field.

BEGIN {OFS= "\t"
print "Owner    PID     CPU     MEM     Process"}
/httpd/ {print $1,$2,$3,$4,$11}
/cron/ {print $1,$2,$3,$4,$11}


awk -f header processes
Owner    PID      CPU    MEM     Process
root     14022    0.0     1.0    /usr/sbin/httpd
apache   14023    0.0     0.7    /usr/sbin/httpd
root     14031    0.0     0.4    crond

awk file: header
Another modification is to use the END section to provide information after the processing is complete.  Here the “NR”, the built-in for number of records, is used with a text string to provide information.  Note the text string is in double quotes to maintain the spacing.

BEGIN {OFS= "\t"
print "Owner    PID     CPU     MEM     Process"}
/httpd/ {print $1,$2,$3,$4,$11}
/cron/ {print $1,$2,$3,$4,$11}
END {print "Total Number of Records Processed:  " NR}

awk -f header processes
Owner     PID      CPU     MEM    Process
root     14022     0.0     1.0     /usr/sbin/httpd
apache   14023     0.0     0.7     /usr/sbin/httpd
root     14031     0.0     0.4     crond
Total Number of Records Processed:  15

Comments on this entry are closed.

Previous post:

Next post: