What Is Awk in Scripting?


Heather Bennett

Awk is a powerful scripting language used for manipulating and processing text files. It is particularly useful for extracting specific information from structured data. In this article, we will explore the basics of Awk and how it can be used to simplify complex data processing tasks.

What is Awk?

Awk was developed in the 1970s at Bell Labs and named after its creators: Alfred Aho, Peter Weinberger, and Brian Kernighan. It is a versatile tool that combines features from programming languages like C, sed, and grep to process and analyze text files.

Awk operates on a line-by-line basis, making it well-suited for working with structured data such as log files, CSV files, or configuration files. It allows you to specify patterns to match within each line of input and perform actions based on those patterns.

Basic Syntax

The basic syntax of an Awk command follows this pattern:

awk 'pattern { action }' input-file

The pattern specifies the condition that must be met for the action to be executed. If no pattern is specified, the action will be applied to all lines of the input file.

The action represents the commands or operations that are performed when the associated pattern matches. Multiple actions can be separated by semicolons.

  • Patterns:
    • /pattern/: Matches lines containing the specified pattern.
    • BEGIN: Specifies actions to be executed before processing any input.
    • END: Specifies actions to be executed after processing all input.
  • Actions:
    • { print }: Prints the entire line.
    • { print $n }: Prints the nth field of the line.
    • { printf(format, arguments) }: Formats and prints output using a specified format.
    • { variable = value }: Assigns a value to a variable.


Let’s look at some examples to understand how Awk works:

Example 1:

Suppose we have a file called “data.txt” with the following content:


We can use Awk to extract and print only the names from this file:

awk -F',' '{ print $1 }' data.txt

This command uses a comma (,) as the field separator (-F option) and prints only the first field ($1) of each line. The output will be:


Example 2:

Let’s say we have a log file called “access.log” with entries like this: - - [10/Jan/2022:12:34:56] "GET /index.html HTTP/1.1" 200 1234
192.2 - - [10/Jan/2022:12:35:01] "POST /login HTTP/1.1" 302 -

We can use Awk to extract and print only the request methods (GET or POST) from this file:

awk '{print $6}' access.log | awk -F'"' '{print $2}'

The first Awk command extracts the 6th field (request method) from each line. The output is then piped (|) to a second Awk command, which uses the double quotes (“) as the field separator to extract only the request method. The final output will be:



Awk is a powerful scripting language that can simplify complex data processing tasks. By combining pattern matching and actions, you can easily extract, manipulate, and analyze data from text files. Understanding the basic syntax and examples provided in this article will give you a solid foundation for using Awk effectively in your scripts.

Remember to experiment with different patterns and actions to fully harness the capabilities of Awk. Happy scripting!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy