• DevOps
    Case Study

    How we built a resilient multi-account, multi-cloud solution for a Health Tech service provider!

    READ CASESTUDY
    icon

    24/7 DevOps as a Service

    Round-the-clock DevOps for uninterrupted efficiency.

    icon

    Infrastructure as a Code

    Crafting infrastructure with ingenious code.

    icon

    CI/CD Pipeline

    Automated CI/CD pipeline for seamless deployments.

    icon

    DevSecOps

    Integrated security in continuous DevOps practices.

    icon

    Hire DevOps Engineers

    Level up your team with DevOps visionaries.

    icon

    Consulting Services

    Navigate success with expert DevOps consulting.

  • TechOps
    Case Study

    How we built a scalable Odoo solution for a Travel Tech service provider!

    READ CASESTUDY

    WEB HOSTING SUPPORT

    icon

    HelpDesk Support

    Highly skilled 24/7 HelpDesk Support

    icon

    Product Support

    Boost your product support with our expertise.

    MANAGED SERVICES

    icon

    Server Management

    Don’t let server issues slow you down. Let us manage them for you.

    icon

    Server Monitoring

    Safeguard your server health with our comprehensive monitoring solutions.

    STAFF AUGMENTATION

    icon

    Hire an Admin

    Transform your business operations with our expert administrative support.

    icon

    Hire a Team

    Augment your workforce with highly skilled professionals from our diverse talent pool.

  • CloudOps
    Case Study

    How we helped a Private Deemed University in India, save US $3500/m on hosting charges!

    READ CASESTUDY
    icon

    AWS Well Architected Review

    Round-the-clock for uninterrupted efficiency

    icon

    Optimize

    Efficient CloudOps mastery for seamless cloud management

    icon

    Manage

    Automated CI/CD pipeline for seamless deployments

    icon

    Migrate

    Upgrade the journey, Migrate & Modernize seamlessly

    icon

    Modernize

    Simplify compliance complexities with our dedicated services

    icon

    FinOps as a Service

    FinOps as a Service

  • SecOps
    Case Study

    How we built a scalable Odoo solution for TravelTech service provider!

    READ CASESTUDY
    icon

    VAPT

    Vulnerability Assessment and Penetration Testing

    icon

    Source Code Review

    Ensuring source code security ans safe practices to reduce risks

    icon

    Security Consultation

    On demand services for improving server security

    icon

    System Hardening

    Reduced vulnerability and proactive protection

    icon

    Managed SoC

    Monitors and maintains system security. Quick response on incidents.

    icon

    Compliance as a Service

    Regulatory compliance, reduced risk

  • Insights
    Case Study

    How we helped a Private Deemed University in India, save US $3,500/m on hosting charges!

    READ CASESTUDY
    icon

    Blog

    Explore our latest articles and insights

    icon

    Case Studies

    Read about our client success stories

    icon

    Flipbook

    Explore our latest Flipbook

    icon

    Events

    Join us at upcoming events and conferences

    icon

    Webinars

    Watch our educational webinar series

  • Our Story
  • Contact Us

Interested to collaborate?

Get in touch with us!

Ready to elevate your business with certified cloud expertise? Contact us today to learn how our team can help you leverage cloud technology to drive growth, streamline operations, and enhance security.

  • AWSAWS
  • Azure CloudAzure Cloud
  • Google CloudGoogle Cloud
  • Akamai CloudAkamai Cloud
  • OVHOVH
  • Digital OceanDigital Ocean
  • HetznerHetzner
  • Kubernetes Consultancy Services
  • K8s & Cloud native Solutions
  • 24/7 Infrastructure Monitoring
  • DevOps as a Service
  • Cloud CI/CD Solutions
  • White Labeled MSP Support
  • Our story
  • Life@SupportSages
  • Insights
  • Careers
  • Events
  • Contact Us

Connect with us!


LinkedInFacebookXInstagramYouTube

aws partneraws advanced partner
SupportSages

Copyright © 2008 – 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy

An Introduction to AWK

Anitta Jose

  • 5 min read
An Introduction to AWK

Generating audio, please wait...

AWK is actually a programming language which is specially designed for processing texts. The name “AWK” is derived from the family name of its authors – Alfred Aho, Peter Weinberger, and Brian Kernighan. AWK is by default available on most Linux distributions. We can check whether it is present by using the “which” command.

AWK is very simple to use. It can be used either directly from the command line or by executing a text file containing AWK commands.

To install AWK on Debian based system, use apt package manager:

sudo apt-get update
sudo apt-get install gawk

On RPM based system, we can use yum package manager to install AWK as shown below:

yum install gawk

Program Structure

The program structure of AWK is as follows:

  • BEGIN Block
  • Body
  • END Block

The following diagram depicts the program structure of AWK in a more precise manner:

awk

BEGIN Block

This is the first section which is executed in the program however it is executed only once. BEGIN is a keyword and hence it should be in uppercase letters. This is mainly used to initialize variables. Also please note that this section is not mandatory, it is optional.

The syntax of BEGIN block is as follows:

BEGIN {awk commands}

BODY Block

This block performs three steps as follows:

 Step  Purpose
 Read  Read each line from the input stream and store in its memory
 Execute  Execute AWK commands on every line. If we need to restrict this, we have to use certain patterns
 Repeat  Repeat the above two steps until the end of the file.

 

The syntax of Body block is as follows:

pattern {actions}

END Block

As its name indicates, it is executed at the end of the program. Here END is a keyword hence it should be in uppercase letters. This block is also optional.

The syntax of END block is as follows:

END {awk-commands}

 

How to execute AWK commands?

As already mentioned above, it can be executed either from command line or by executing a text file. To specify AWK commands in command line, use the following pattern:

awk [options] file

Here the “file” specifies the file on which AWK commands are to be executed. Consider the following example for more clarification.

Example 1: Consider a text file list.txt as follows:

list

Suppose we need to print the entire contents of this text file (list.txt) containing the list of grocery items and its quantity. The AWK command to execute the same is as follows:

awk '{print}' list.txt

In order to execute the AWK commands which are given in a file, use the following pattern:

awk [options] -f file

Example 2:

To execute the above example by executing commands in a text file, we should initially create a text file (commands.awk) which contains the command:

{print}

Now we can instruct AWK to read commands from text file and then perform the required task. This can be achieved by using the -f option along with the awk command.

awk -f commands.awk list.txt

Some other examples of awk are as follows:

Example 3:

Print the second column of list.txt

awk '{print $2}' list.txt

Here $2 represents second column. $1, $2, $3… represents the first, second, third columns… in a row respectively. In order to print an entire row, use $0. In the above example, it prints the second column in each row.

If we need to print 2nd and 3rd columns, use the command:

awk '{print $2, $3}' list.txt

Example 4:

Use ‘if‘ command with awk

awk '{if ($1=="2") print $0;}' list.txt

Here it checks whether 1st column matches with “2”. If yes, then it prints that entire row.

Example 5:

Use ‘for‘ command with awk

awk 'BEGIN { for(i=1;i<=5;i++) print "Cube of", i, "is",i*i*i; }'

The output will be as follows:

for

Built-in Variables

The built-in variables used in awk are as follows:

 Variables  Purpose
 0, $1, $2, …  Entire row, first column, second column, …
 FS  Input field separator
 OFS  Output field separator
 NF  Number of fields
 NR  Number of records

 

I believe now you have got a basic idea about Awk and its uses. It is actually a very powerful filter. Here I have mentioned the basic features/uses only, it is even more and can only be made handy with practice.

Get 24/7 expert server management

  • Linux
  • Training
An Introduction to AWK

“SED” Options and its usage

“SED”  Options and its usage
  • Howtos
  • Linux
logo

Configure Disk Quota on Ubuntu Server

Configure Disk Quota on Ubuntu Server
  • Linux
logo

How to check whether UDP connection is open or not?

How to check whether UDP connection is open or not?
  • Linux
logo

How to Enable HTTP redirection in IIS Servers

How to Enable HTTP redirection in IIS Servers
  • IIS
logo

Posts by Anitta Jose

Anitta is systems engineer since 2015 and holds broad experience in Linux, WordPress, and cPanel systems administration. Her interest lies more in Cloud technologies (AWS). From 2016, she writes blogs to share her experiences with wider audience.