Hello World in AWK
Your first AWK program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first AWK program and get a glimpse of this legendary text-processing language from Bell Labs.
The Code
Create a file named hello.awk:
| |
That’s it! One line of AWK code.
Understanding the Code
This simple program demonstrates AWK’s fundamental structure:
BEGIN- A special pattern that matches before any input is read{ print "Hello, World!" }- The action block that executes when the pattern matchesprint- AWK’s output statement (adds a newline automatically)
The Pattern-Action Model
AWK programs consist of pattern-action pairs:
pattern { action }
The BEGIN pattern is special - it always matches once, before any input is processed. This makes it perfect for printing messages that don’t depend on input data.
Running with Docker
The easiest way to run AWK without any system-specific concerns:
| |
What These Commands Do
docker pull alpine:latest- Downloads a minimal Linux image (AWK is included)docker run --rm -v $(pwd):/app -w /app- Runs a container with your current directory mountedawk -f hello.awk- The AWK interpreter runs your program file
Why Alpine Linux?
Alpine Linux includes BusyBox AWK by default, making it perfect for running AWK scripts in a tiny container (around 5MB). For more complex AWK programs, you might want GNU AWK (gawk), but BusyBox AWK handles our Hello World perfectly.
Running Locally
AWK is available on virtually every Unix-like system:
macOS
| |
Linux
| |
Windows (with Git Bash or WSL)
| |
Expected Output
Hello, World!
Alternative Approaches
Command-Line One-Liner
AWK excels at one-liners. You can run Hello World without a file:
| |
This is AWK’s most common usage pattern - quick one-liners in the terminal.
Using Input Data
Traditional AWK programs process input, so here’s a version that expects input:
| |
This prints “Hello, World!” for every line of input. Run it with:
| |
Multiple Patterns
AWK programs can have multiple pattern-action pairs:
| |
This prints:
Hello, World!
Goodbye!
The END pattern matches after all input is processed.
Using Variables
AWK supports variables:
| |
Note: In AWK, string concatenation is done by simply placing strings adjacent to each other (with or without spaces).
AWK Program Structure
A complete AWK program can have:
| |
For Hello World, we only need BEGIN since we’re not processing any input.
Key Concepts
- No main function - AWK has an implicit main loop over input
- Pattern-action pairs - The fundamental building block
- Special patterns -
BEGINandENDfor setup and teardown - print statement - Simple output with automatic newline
- No compilation needed - AWK is interpreted
The -f Flag
The -f flag tells AWK to read the program from a file:
| |
Without -f, the program is the first argument:
| |
Why BEGIN?
For Hello World, we use BEGIN because:
- We don’t need to process any input
- We want the message to print exactly once
- It executes immediately when AWK starts
Without BEGIN, AWK would wait for input and print “Hello, World!” for each input line.
Historical Note
AWK was created in 1977 at Bell Labs by Alfred Aho, Peter Weinberger, and Brian Kernighan - the initials AWK come from their surnames. It was designed to fill a gap between grep (searching) and sed (stream editing) for processing structured text data.
The language was so successful that it influenced the design of Perl, which Larry Wall described as “sed and awk and shells and lots of other things thrown together.”
Next Steps
Now that you’ve run your first AWK program, explore:
- Field processing with
$1,$2, etc. - Pattern matching with regular expressions
- Built-in variables like
NF(number of fields) andNR(record number) - Associative arrays for counting and grouping
- User-defined functions
AWK’s power shines when processing structured text data - logs, CSV files, configuration files, and more. The pattern-action paradigm makes complex text transformations surprisingly concise.
Welcome to AWK - where text processing becomes elegant!