Mastering File And Text Manipulation With awk | Bash

awk is a powerful tool for text processing in Unix-like systems. It excels at manipulating and analyzing text files with structured data, particularly in tabular formats. This guide will explore awk's capabilities through explanations and examples.

Basic Syntax:
awk 'pattern { action }' file
  1. pattern:An optional regular expression to match lines (if omitted, all lines are processed).
  2. action:The commands to execute on matching lines (enclosed in curly braces).
  3. file:The input file to process (optional, reads from standard input by default).

Key Concepts:

  1. Fields:awk divides each line into "fields" based on whitespace by default. You can change the separator with -F.
  2. Variables:Use built-in variables like $0 (entire line), $1 (first field), $NF (last field), etc.
  3. Operators:awk supports arithmetic, string manipulation, and logical operators.
  4. Conditionals:Use if statements for conditional actions.
  5. Loops:Use for and while loops for repetitive tasks.

Printing Columns

awk '{print $2, $4}' data.txt

This command prints the second and fourth columns of the file data.txt.

Filtering Rows

awk '$3 > 50 {print $1, $3}' data.txt

Prints the first and third columns for rows where the value in the third column is greater than 50.


awk '{total += $2} END {print "Total:", total}' data.txt

Calculates the sum of values in the second column and prints the total at the end.

Pattern Matching

awk '/pattern/ {print $1, $3}' data.txt

Prints the first and third columns for lines that contain the specified pattern.

Field Separator

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

Specifies a comma as the field separator for a CSV file. Prints the first and last columns.

Custom Actions

awk '{if ($2 > 50) print $1 " is high"; else print $1 " is low"}' data.txt

Uses an if-else statement to classify values in the second column as high or low.

Formatting Output

awk '{printf "%-10s %-8s\n", $1, $2}' data.txt

Formats and prints the first and second columns with specified width and alignment.

Multiple Commands

awk '{print $1} NR==2 {print "Second line"}' data.txt

Executes the first command for every line and the second command only for the second line.

Combining with other Commands

ls -l awk '{print $9, $5}'

Uses Awk to process the output of ls -l and prints the file names and their sizes.

Count lines starting with "error"

awk '/^ error/' data.txt wc -l

Count lines matching "^ error" pattern

Replace "error" with "warning"

awk 'gsub(/error/, "warning") {print}' data.txt

Replace "error" with "warning" in each line

Calculate average of a column

awk '{ sum += $3 } END { print sum / NR }' data.txt

Calculate average of the third field

Filter lines based on conditions

awk '$2 > 10 { print }' data.txt

Print lines where the second field is greater than 10

Use multiple patterns and actions

awk '/error/ { print "Error on line:", NR } /warning/ { print "Warning on line:", NR }' data.txt

Process multiple files and aggregate results

awk 'sum += $2 { total += sum } END { print "Total sum:", total }' file1.txt file2.txt

Beyond Basics

User-Defined Functions

Awk supports user-defined functions, enabling users to create custom operations for complex tasks. This feature enhances the language's flexibility and allows users to extend its capabilities based on specific requirements.

String Manipulation Functions

Awk provides built-in string manipulation functions such as length, substr, and match. These functions simplify text processing tasks, allowing users to extract substrings, find matches, or determine the length of strings within Awk scripts.

Interacting with Other Commands

Awk seamlessly interacts with other commands through pipes and standard input/output. This capability allows users to integrate Awk into more extensive command-line pipelines, facilitating the combination of different tools for efficient and powerful text processing workflows.


Awk is a powerful text processing tool with a concise and expressive syntax. It's particularly useful for tasks involving structured text data. The examples provided cover some common use cases, but Awk's capabilities extend to more complex scenarios, making it a valuable tool in the Unix/Linux command-line environment.