Tutorial For Beginners: Learn awk Command with Examples in Linux

AWK is an interpreted programming language authored by Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan. It is a powerful tool for text processing and report generation. It filters information from raw data based on a given pattern provided by the user.  Patterns may be numerals, alphabets, special characters, or a combination of these.

In simple terms, we will use awk to filter data and convert it into actionable information. A perfect example of usage is to extract meaningful information from logs of servers, network devices etc.

AWK is generally pre-installed on the Linux-based system. To identify the absolute path of awk on our system:

$which awk

You can also use the below command to find the awk version on the system:

$awk --version

We will use test-file.txt as a raw data.

sample file: test-file.txt

How does awk perform its operation?

awk scans each line of input data and converts it into fields based on a given pattern by the user. It displays formatted information based on a given pattern.

Click Here to know Conditional and Looping Statements in AWK Programming Language

Basic Syntax

awk [options] [input-file]

Here, we will discuss the usage of the awk command with examples. This will help you in experimenting with the commands on different input data or files.

Usage Examples of Linux awk Command

(1) Print the whole content

$awk '{print}' test-file.txt

(2) Print string

$awk 'BEGIN { print "Don\47t !" }'

(3) Print lines that match the string

$awk '/Daily/ {print}' test-file.txt

(4) Print lines that match any string

$awk '/systemd|exit/ {print}' test-file.txt

(5) Print content first and fourth field/column

$ awk '{print $1,$4}' test-file.txt

(6) Print all of the content

$ awk '{print $0}' test-file.txt

(7) Zero or One character matches

$echo -e "<string1\nstring2\nstring3\nstring4>" | awk  '/st?r/'

(8) Print all content of even number lines available

$awk 'NR % 2 == 0' test-file.txt

(9) Print the line number in front of every line

$ awk '{print NR,$0}' test-file.txt

(10) Display the first and Last Field/column

$ awk '{print $1,$NF}' test-file.txt

(11) Display Line From 3 to 6 of the file

$ awk 'NR==3, NR==6 {print NR,$0}' test-file.txt

(12) Print the first column which contains the string "sbin"  separated by a colon

$awk -F: '/sbin/ { print $1 }' /etc/passwd

(13) Using expressions in field number 

$ awk '{ print $(2*3) }' test-file.txt

(14) Count the no of lines

$ awk 'END { print NR }' testfile.txt

(15) Find the length of the longest line

$ awk '{ if (length($0) > max) max = length($0) } END { print max }' test-file.txt

(16) To print line which has more than 5 no of fields/column

$ awk 'NF > 5' test-file.txt

(17) Print content of file based on field delimiter

$awk -Fubuntu '{ print $1 }' test-file.txt | sort

(18) Print seven random numbers

$awk 'BEGIN { for (i = 1; i <= 7; i++)
print int(101 * rand()) }'

Conclusion

AWK is the most powerful tool used for text processing and report generation. This tutorial gives you a start and later you can easily apply those commands to extract information from raw data. Here, I have mentioned the usage of the awk command with examples.

Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

You may also like...

1 Response

  1. Shavonne says:

    I’ve been exploring for a little for any high quality articles
    or blog posts on this kind of space . Exploring in Yahoo I ultimately
    stumbled upon this web site. Studying this information So i’m glad to convey that
    I have a very just right uncanny feeling I came upon exactly what I needed.
    I so much definitely will make certain to do not put out of your
    mind this website and give it a glance on a constant basis.

Leave a Reply

Your email address will not be published. Required fields are marked *

10 Blockchain Security Vulnerabilities OWASP API Top 10 - 2023 7 Facts You Should Know About WormGPT OWASP Top 10 for Large Language Models (LLMs) Applications Top 10 Blockchain Security Issues