Use AWK Command with Examples on Linux

This guide intends to teach you to use AWK Command with Examples on Linux.

The awk command is used for text processing in Linux. The Awk is a powerful scripting language used for text scripting. It searches and replaces the texts and sorts, validates, and indexes the database.

It is one of the most widely used tools for programmers, as they write the scaled-down effective program in the form of a statement to define the text patterns and designs.

Steps To Use AWK Command with Examples on Linux

To complete this guide, you need to log in to your Linux distro and follow the steps below.

Install AWK Comamnd on Centos / RHEL / Fedora

To install the AWK command line utility on CentOS/Fedora or any other RPM-based Linux distribution, you can use the following command:

yum install gawk

Install AWK Comamnd on Ubuntu / Debian

To install the AWK command on Ubuntu or Debian-based distros, you can use the following command:

apt install gawk

Examples of AWK Command

At this point, we want to show you some useful examples of awk commands and executable scripts on Linux.

To print only certain columns from the input field, you can use the awk command.

For example, you can find out the list of IP addresses that are connected to your server:

netstat -anp|grep tcp|awk '{print $5}'| cut -d : -f1 | sort | uniq -c | sort -n

And you can use the awk command to search for a particular pattern in certain columns and do some action based on the result. For example:

exim -bpr | grep frozen | awk {'print $3'} | xargs exim -Mrm

This will delete all frozen emails from the Exim mail queue.

Another usage of the AWK command is to remove duplicates in a text file without sorting. For example:

awk '!x[$0]++' file-with-duplicates > new-file-without-duplicates

To find five random numbers from 0 to 999, you can use:

awk 'BEGIN { for (i = 1; i <= 5; i++) print int(1000 * rand()) }'

If you want to find the number of lines in a file, you can use the following awk command:

awk 'END { print NR }' sample_file

Also, you can find specific lines in a file. For example, find lines that start with A or a followed by re:

awk '/[Aa]re/{print}' /opt/sample_file

Complex Operations with AWK Command

You can use the AWK command for more complex operations. If your website looks slow, you can use the following command to check if there is some problem with the disk I/O or network:

tac /proc/stat | awk '/^btime/ {up=systime()-$2;print "up " up/86400 "d"}; /^cpu / {print "user " $2/up "%, nice " $3/up "%, sys " $4/up "%, idle " $5/up "%, iowait " $6/up "%, steal " $9/up "%\niowait/used " $6 / ($2+$3+$4) ", steal/used " $9 / ($2+$3+$4) }'

IOWAIT means how long processes are blocked by busy I/O, mostly disk storage or perhaps network.

STEAL means how long processes are blocked by lack of CPU timeslice on the server.

Higher iowait perused CPU time (=USER + NICE + SYSTEM) shows busy I/O and higher STEAL perused shows busy CPU.

The following script uses a simple awk command that searches the input file ‘/etc/passwd’ and provides an output with the username followed by the date and time of the last login:

vi login-check
#!/bin/bash

for user in `awk -F: '{print $1}' /etc/passwd`
do
echo -n "$user: "
finger $user | grep Last
if [ $? != 0 ]; then
echo
fi
done

Save and close the file.

Make the script executable:

chmod 755 login-check

Run the script:

./login-check

You should be able to see the user accounts available on your server, followed by the date and time of the last login of each user.

Conclusion

At this point, you have learned to Use AWK Command with Examples on Linux.

Hope you enjoy it. You may be interested in these articles:

Run Binary Files on Linux

Fix Error Failed to load SELinux policy freezing

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!