Skip to content
Home

Awk (programming language)

Awk is a compact, domain-specific language for processing and extracting text and tabular data, commonly used in Unix-style environments for scripting, reporting and quick data transformations.

Overview

Awk is a small, domain-specific programming language designed for processing and extracting information from text files and streams. It operates on a pattern-action model: when an input record matches a pattern, the associated action is executed. Because of its concise syntax and built-in text-handling features, Awk is frequently used in command-line pipelines and short scripts for data filtering and reporting. For more detailed references see Awk resources.

Image gallery

1 Image

Key characteristics

Awk treats input as records (usually lines) split into fields. Common built-ins include $1, $2 for fields, NF for number of fields, and NR for record number. The language integrates regular expression matching, associative arrays (hashes), arithmetic and string operations, and user-defined functions in many implementations. Its default behavior makes simple transformations easy: for example, awk '{print $1}' prints the first field of each line.

History and development

Awk was created as a tool for text-processing tasks in the early Unix tradition and is named after its original authors. Over time it evolved into several implementations and dialects; GNU Awk (gawk) adds extensions beyond the original specification, while other implementations focus on speed or portability.

Common uses and examples

Awk excels at extracting columns from delimited files, generating simple reports, and transforming logs. Typical uses include splitting fields with a custom separator (for example -F), computing column totals, or selecting records that match patterns. It is often combined with other Unix tools in shell pipelines to perform quick data extraction and cleanup tasks. Users seeking tutorials or sample scripts may consult general data-extraction guides at data extraction resources.

Variants, tooling, and notable facts

  • Popular implementations: original Awk, nawk (new Awk), gawk (GNU Awk), mawk.
  • Gawk includes extensions such as additional functions, networking, and better internationalization.
  • Awk is valued for readability and brevity in one-liners and small scripts, but larger programs may be ported to general-purpose languages for maintainability.

Structure of a program

The typical execution of an awk program consists of performing operations - such as substitutions - on an input text. To do this, the text is read in line by line and split into fields using a chosen separator - usually a series of spaces and/or tab characters. Afterwards, the awk statements are applied to the respective line.

awk statements have the following structure:

  Condition { statement block }

For the line read in, it is determined whether it satisfies the condition (often a regular expression). If the condition is met, the code is executed within the statement block enclosed by curly braces. Deviating from this, a statement can also consist of only one action

  { statement block }

or only from a condition

  Condition

exist. If the condition is missing, the action is executed for each line. If the action is missing, the writing of the entire line is executed as the default action, provided that the condition is fulfilled.

Variables and functions

The user can define variables within statement blocks by referencing, an explicit declaration is not necessary. The scope of the variables is global. An exception here are function arguments, whose validity is restricted to the function defining them.

Functions can be defined at any position, the declaration does not have to be made before the first use. If scalars are involved, function arguments are passed as value parameters, otherwise as reference parameters. The arguments when calling a function do not have to match the function definition, excess arguments are treated as local variables, omitted arguments are given the special value uninitialized - numerically zero and as a string the value of the empty string.

Functions and variables of all kinds use the same namespace, so identical naming leads to undefined behavior.

In addition to user-defined variables and functions, standard variables and standard functions are also available, for example the variables $0 for the entire line, $1, $2 ... for the i-th field of the line and FS (from field separator) for the field separator, as well as the functions gsub(), split() and match().

Related articles

Author

AlegsaOnline.com Awk (programming language)

URL: https://en.alegsaonline.com/art/7821

Share