Skip to main content

Command Palette

Search for a command to run...

AWK Syntax Essentials

Updated
8 min read
AWK Syntax Essentials

Syntax is based on The AWK Programming Language, 2nd Edition by Alfred V. Aho, Brian W. Kernighan, and Peter J. Weinberger.

Pattern

A pattern determines when an action is executed. When a pattern matches an input line, its associated action is executed.

If no action is specified, the default is print $0.

Syntax: pattern { action }

Examples

  1. BEGIN — runs once before any input is read
BEGIN { FS=":" }
  1. END — runs once after all input is processed
END { print "total:", total }
  1. Expression — executes on every input line where the condition is true

    Skip the header line

NR > 1 { print $0 }
  1. Regex — executes on every line matching the pattern
/error/ { print $0 }
  1. Range — matches all lines from pattern1 through pattern2, inclusive
/start/,/end/ { print $0 }

Conditionals

AWK supports standard conditionals for branching logic.

Examples

  1. if

    Skip empty lines

if (NF > 0) print $0
  1. if-else
if ($1 > 0) print "positive"; else print "negative"
  1. if-else if
if ($1 > 0) print "positive"
else if ($1 < 0) print "negative"
else print "zero"

Ternary Expression

The ternary operator (?:) is a compact, C-style alternative to the if-else statement. By using this operator, a concise one-line ternary expression can be constructed, which, unlike a statement, returns a value that can be used directly within calculations or commands.

Syntax: condition ? value_if_true : value_if_false

Examples

  1. Absolute Value

AWK lacks a built-in abs() function. The ternary operator handles this math logic efficiently:

$1 = ($1 < 0) ? -$1 : $1
  1. Truthiness & Success Labels

AWK treats 0 and "" as false, and everything else as true. Use this to label status codes:

print $1, ($1 ? "SUCCESS" : "FAILURE")

or

printf("%s\t%s\n", $1, ($1 ? "SUCCESS" : "FAILURE"))

Associative Arrays

AWK arrays are associative — keys can be strings or numbers, making them ideal for counting, grouping, and lookups without any pre-declaration.

Syntax: array[key] = value

Examples

  1. Populate an array with lines
x[NR] = $0
  1. Deduplicate lines
!x[$0]++
  1. Populate a 2D matrix with all fields
for (i=1; i<=NF; i++) x[NR, i] = $i

AWK simulates 2D arrays by concatenating keys with a built-in separator (SUBSEP), so x[row, col] is stored internally as x[row SUBSEP col].

Loops

AWK supports standard C-style loops as well as a dedicated form for traversing associative arrays.

Examples

  1. for loop
for (i=1; i<=NF; i++) print $i
  1. while loop
while (i <= NF) { print $i; i++ }
  1. do-while loop
do { print $i; i++ } while (i <= NF)
  1. Iterate over an associative array
for (k in array) print k, array[k]

Pipes

AWK can send output to, or receive input from, external shell commands using pipes, enabling seamless integration with standard Unix tools.

Syntax: command | getline [var] / print | "command"

Examples

  1. Send output to a shell command
print $1 | "sort"
  1. Read a shell command's output into a variable
"date" | getline today
  1. Pipe to multiple commands (close between uses)
print $1 | "sort | uniq -c"

Comparison Operators

OperatorMeaning
<less than
<=less than or equal to
==equal to
!=not equal to
>=greater than or equal to
>greater than
~matched by
!~not matched by

Logical Operators

OperatorMeaning
&&AND
││OR
!NOT

Syntax: condition1 operator condition2

Built-in variables

VariableDescriptionDefault
ARGCNumber of command-line arguments, including command name-
ARGVArray of command-line arguments, numbered 0..ARGC-1-
CONVFMTConversion format for numbers"%.6g"
ENVIRONArray of shell environment variables-
FILENAMEName of current input file-
FNRRecord number in current file-
FSInput field separator" "
NFNumber of fields in current record-
NRNumber of records read so far-
OFMTOutput format for numbers"%.6g"
OFSOutput field separator for print" "
ORSOutput record separator for print"\n"
RLENGTHLength of string matched by match function-
RSInput record separator"\n"
RSTARTStart of string matched by match function-
SUBSEPSubscript separator"\034"

Built-in Arithmetic Functions

FunctionValue Returned
atan2(y, x)arctangent of y/x in the range −π to π
cos(x)cosine of x, with x in radians
exp(x)exponential function of x, e^x
int(x)integer part of x; truncated towards 0
log(x)natural (base e) logarithm of x
rand()random number r, where 0 ≤ r < 1
sin(x)sine of x, with x in radians
sqrt(x)square root of x
srand(x)x is new seed for rand(); use time of day if x is omitted; return previous seed

Built-in String Functions

FunctionDescription
gsub(r,s)substitute s for r globally in $0, return number of substitutions made
gsub(r,s,t)substitute s for r globally in string t, return number of substitutions made
index(s,t)return first position of string t in s, or 0 if t is not present
length(s)return number of Unicode characters in s; return number of elements if s is an array
match(s,r)test whether s contains a substring matched by r; return index or 0; sets RSTART and RLENGTH
split(s,a)split s into array a on FS or as CSV if --csv is set, return number of elements in a
split(s,a,fs)split s into array a on field separator fs, return number of elements in a
sprintf(fmt,expr-list)return expr-list formatted according to format string fmt
sub(r,s)substitute s for the leftmost longest substring of $0 matched by r; return number of substitutions made
sub(r,s,t)substitute s for the leftmost longest substring of t matched by r; return number of substitutions made
substr(s,p)return suffix of s starting at position p
substr(s,p,n)return substring of s of length at most n starting at position p
tolower(s)return s with upper case ASCII letters mapped to lower case
toupper(s)return s with lower case ASCII letters mapped to upper case

Expression Operators

OperationOperatorsExampleMeaning of Example
assignment= += -= *= /= %= ^=x *= 2x = x * 2
conditional?:x ? y : zif x is true then y else z
logical OR││x ││ y1 if x or y is true, 0 otherwise
logical AND&&x && y1 if x and y are true, 0 otherwise
array membershipini in a1 if a[i] exists, 0 otherwise
matching~ !~$1 ~ /x/1 if the first field contains an x, 0 otherwise
relational< <= == != >= >x == y1 if x is equal to y, 0 otherwise
concatenation(none)"a" "bc""abc"; there is no explicit concatenation operator
add, subtract+ -x + ysum of x and y
multiply, divide, mod* / %x % yremainder of x divided by y
unary plus and minus+ --xnegated value of x
logical NOT!!$11 if $1 is zero or null, 0 otherwise
exponentiation^x ^ yx to the power y
increment, decrement++ --++x, x++add 1 to x
field$$i + 1value of i-th field, plus 1
grouping()$(i++)return i-th field, then increment i

printf

printf format-control characters

CharacterPrint Expression As
csingle UTF-8 character (code point)
d or idecimal integer
e or E[-]d.dddddde[+-]dd or [-]d.ddddddE[+-]dd
f[-]ddd.dddddd
g or Ge or f conversion, whichever is shorter, with nonsignificant zeros suppressed
ounsigned octal number
uunsigned integer
sstring
x or Xunsigned hexadecimal number
%print a %; no argument is consumed