Use AWK Command with Examples on Linux – Best 3 Steps

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.

You can now proceed to the following guide provided by the Orcacore team to install and use AWK command with examples on Linux.

Steps To Use AWK Command with Examples on Linux

To Use AWK Command with Examples on Linux, you need to log in to your Linux distro and follow the steps below.

AWK Command In Linux

Step 1 – Install AWK Command on Centos / RHEL / Fedora

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

# yum install gawk -y
Or
# dnf install gawk -y

Step 2 – Install AWK Command on Ubuntu / Debian

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

apt install gawk -y

Step 3 – Examples of Using AWK Command

At this point, we want to show you how to use AWK Command with Examples on Linux and executable scripts.

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

Search For a Particular Pattern In Certion Columns with AWK Command

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.

Remove Duplicates in a File with AWK Command

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

Find Numbers and Lines with AWK Command

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 AWK Command with Examples on Linux:

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 AWK Command with Examples on Linux 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. Understanding how to use AWK can greatly enhance your ability to perform complex text-processing tasks directly from the command line. It allows users to extract and manipulate text from files or input streams by defining patterns and actions. AWK is especially useful for tasks like data extraction, generating reports, and formatting text.

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

Run Binary Files on Linux

Fix Error Failed to load SELinux policy freezing

Fix cPanel Error ‘PHP PECL imagick fails on PHP 8.3’

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!