Skip to main content
Awk is a powerful, domain-specific language built for efficient text processing. In this guide, we’ll cover its core syntax, the pattern-action structure, and how to use the print statement to extract and format data.

Awk Usage Overview

Always quote your program (single or double quotes) so the shell passes it verbatim to awk.
For full details, see the GNU Awk Manual.

Pattern-Action Structure

An awk program is a sequence of pattern-action pairs:
  • If pattern is omitted, action runs on every input line.
  • The { … } block is the action block, containing commands like print, loops, and conditionals.
Example: Start an interactive session that does nothing with your input.
Type lines, then press Ctrl-D to end input:
Without quotes around {}, many shells will interpret braces or special characters—always quote your action blocks!

The print Statement

Inside the action block, print sends its arguments (fields, string literals, variables) to standard output.

Accessing Fields

By default, awk splits each line on whitespace into fields named $1, $2, …, $NF.

Processing Files

Place the filename after the program to read from a file instead of interactively: Given abc.txt:
Run:
Output:

Printing String Literals

You can mix fields and literal strings in a single print:
Output:

Multiple Expressions

Separate expressions by commas; awk joins them with the output field separator (OFS, default is a space):

Redirecting and Piping Input

Awk accepts input from:
  • Files:
  • Standard input via redirection:
  • Piping from other commands:

Summary

  • Command structure: awk [options] [pattern-action] [file...]
  • Pattern-action: pattern { action }
  • Fields: $1, $2, … $NF
  • String literals: printed as-is within quotes
  • Separators: input (FS) and output (OFS)
  • Interactive mode: omit files; end with Ctrl-D, cancel with Ctrl-C

Watch Video