awk is a domain-specific language and command for text processing available on Unix-compatible systems. gawk is the GNU AWK and all Linux distributions come with it. This is a brief tutorial for awk covering the most common use-cases.

awk reads input line by line from a file, pipe, or stdin and executes a program on each line. An input line has a number of fields separated by white space or by regular expression FS. The fields are denoted $1, $2, and so on and $0 denotes the entire line. If FS is not set, the input line is split into one field per character.

awk <options> <program> <input_file>

Options

Here are some options for awk command that you most likely need to know about them.

OptionDescription
-fSpecifies the file containing the awk program.
-FSpecifies the regular expression for separating fields.

Actions

An action is a sequence of statements.

ActionDescription
printPrints data on standard output.
printfPrints data on standard output.

Examples

ExampleDescription
awk 'BEGIN {print "Hello, World!"}'Prints Hello, World! on stdout.
awk -F '\t' '{print $2, $4}'Prints the second and fourth columns separated by tab.

Read More