Beginner

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:

1
BEGIN { print "Hello, World!" }

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 matches
  • print - 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:

1
2
3
4
5
# Pull the Docker image
docker pull alpine:latest

# Run the program
docker run --rm -v $(pwd):/app -w /app alpine:latest awk -f hello.awk

What These Commands Do

  1. docker pull alpine:latest - Downloads a minimal Linux image (AWK is included)
  2. docker run --rm -v $(pwd):/app -w /app - Runs a container with your current directory mounted
  3. awk -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

1
2
# AWK is pre-installed (nawk)
awk -f hello.awk

Linux

1
2
# AWK is pre-installed (gawk or mawk)
awk -f hello.awk

Windows (with Git Bash or WSL)

1
awk -f hello.awk

Expected Output

Hello, World!

Alternative Approaches

Command-Line One-Liner

AWK excels at one-liners. You can run Hello World without a file:

1
awk 'BEGIN { print "Hello, World!" }'

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:

1
{ print "Hello, World!" }

This prints “Hello, World!” for every line of input. Run it with:

1
echo "" | awk '{ print "Hello, World!" }'

Multiple Patterns

AWK programs can have multiple pattern-action pairs:

1
2
BEGIN { print "Hello, World!" }
END { print "Goodbye!" }

This prints:

Hello, World!
Goodbye!

The END pattern matches after all input is processed.

Using Variables

AWK supports variables:

1
2
3
4
5
BEGIN {
    greeting = "Hello"
    target = "World"
    print greeting ", " target "!"
}

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:

1
2
3
BEGIN { setup code }      # Runs before input
/pattern/ { action }      # Runs for matching lines
END { cleanup code }      # Runs after all input

For Hello World, we only need BEGIN since we’re not processing any input.

Key Concepts

  1. No main function - AWK has an implicit main loop over input
  2. Pattern-action pairs - The fundamental building block
  3. Special patterns - BEGIN and END for setup and teardown
  4. print statement - Simple output with automatic newline
  5. No compilation needed - AWK is interpreted

The -f Flag

The -f flag tells AWK to read the program from a file:

1
awk -f program.awk input.txt

Without -f, the program is the first argument:

1
awk 'program' input.txt

Why BEGIN?

For Hello World, we use BEGIN because:

  1. We don’t need to process any input
  2. We want the message to print exactly once
  3. 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) and NR (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!

Running Today

All examples can be run using Docker:

docker pull alpine:latest
Last updated: