LinuxCommandLibrary

mawk

Process text files, line by line

SYNOPSIS

mawk [-W option] [-F value] [-v var=value] [--] 'program' [file ...]
mawk [-W option] [-F value] [-v var=value] [-f program-file] [--] [file ...]

PARAMETERS

-F value
    Sets the input field separator to value.

-v var=value
    Assigns the value to variable var before execution of the program begins.

-f program-file
    Reads the AWK program source from the file program-file instead of the command line.

--
    Signals the end of options. Useful if a filename starts with a hyphen.

'program'
    The AWK program to be executed. Must be enclosed in single quotes.

[file ...]
    One or more input files to be processed. If no files are specified, mawk reads from standard input.

-W version
    Displays mawk's version and copyright information.

-W dump
    Produces a byte code listing of the internal representation of the program. This is a debugging aid.

-W interactive
    Unimplemented and reserved for future use.

-W exec
    Unimplemented and reserved for future use.

DESCRIPTION

mawk is an interpreter for the AWK programming language. It is a new awk meaning it implements the AWK language as defined in Aho, Weinberger, and Kernighan, The AWK Programming Language, Addison-Wesley Publishing Company, 1988. mawk conforms to the POSIX 1003.2 (draft 11.3) definition of the AWK language. It implements a few extensions not found in the AWK book, and provides good performance because it compiles the AWK program internally before executing it. It is designed to be a fast and efficient implementation of AWK, often outperforming other AWK implementations like gawk. mawk is particularly useful for processing text files, performing data extraction, and generating reports. It reads input line by line and executes a set of actions based on patterns matched in each line.

CAVEATS

mawk has some limitations compared to other AWK implementations like gawk. For instance, mawk might not support all the features or extensions available in gawk. It's important to test your AWK scripts with mawk to ensure compatibility. Because mawk compiles the AWK program before execution, it tends to use more memory for very long scripts.

BEGIN AND END BLOCKS

AWK programs can contain BEGIN and END blocks. The BEGIN block is executed before the first input line is read, and the END block is executed after the last input line has been processed. These blocks are useful for initializing variables and performing cleanup operations.

FIELD SEPARATORS

The field separator (FS) is used to split each input line into fields. By default, FS is a space or tab character. You can change FS using the -F option or by assigning a value to the FS variable within the AWK program. Example: mawk -F: '{print $1}' /etc/passwd

ARRAYS

mawk supports associative arrays, which are arrays indexed by strings. This allows you to store and retrieve data based on meaningful keys. They can be used to generate frequency tables, store and manipulate data. Example mawk '{counts[$1]++} END {for (word in counts) print word, counts[word]}' file.txt

HISTORY

mawk was created by Mike Brennan and is known for its speed and efficiency compared to older AWK implementations. It was designed to be a lightweight and performant alternative. It's widely used in environments where performance is critical and a minimal footprint is desired. Development has been relatively stable over the years, focusing on bug fixes and compatibility.

SEE ALSO

awk(1), gawk(1), sed(1)

Copied to clipboard