Skip to content

/errors/awk/awk-cmd-line-1-syntax-error

awk: cmd. line:1: syntax error

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · GNU Awk 5.4.1updated
tool
awk
exit code
1
versions
GNU Awk 4.x – 5.x
os
linux, macos, windows

awk: cmd. line:1: ... syntax error is GNU awk (gawk) saying it could not parse the program given on the command line. The next line repeats the program and a ^ points to where parsing failed. The most common causes are a statement outside braces (awk 'print $1' instead of awk '{print $1}'), a missing } or quote, and double quotes that let the shell expand $1 before awk runs. Write the program in single quotes, with every action inside { }.

Reproduce it

bash
echo "alice 42" | awk 'print $1'

Output · exit 1

awk: cmd. line:1: print $1
awk: cmd. line:1: ^ syntax error

A missing closing brace gives a related message that points at the end of the program:

bash
echo "alice 42" | awk '{print $1'

Output · exit 1

awk: cmd. line:1: {print $1
awk: cmd. line:1:          ^ unexpected newline or end of string

Fix: braces and single quotes

bash
echo "alice 42" | awk '{print $1}'
echo "alice 42" | awk '$2 > 40 {print $1, "is over 40"}'

Output

alice
alice is over 40

A pattern without an action ($2 > 40) is valid and prints matching lines; an action without braces is not.

Double quotes: no error, wrong result

With double quotes the shell replaces $1 with its own (usually empty) first argument, so awk receives {print } and prints whole lines:

bash
echo "alice 42" | awk "{print $1}"
echo "alice 42" | awk '{print $1}'

Output

alice 42
alice

To use a shell variable inside the program, pass it with -v:

bash
min=40
echo "alice 42" | awk -v min="$min" '$2 > min {print $1}'

Output

alice

Where the error points

ProgramProblem
awk 'print $1'Action without braces
awk '{print $1'Missing }; gawk reports unexpected newline or end of string
awk '{if ($1 = = 1) print}'Invalid operator (= = instead of ==)
awk '{print $1}}'Extra closing brace
awk "{print $1}"No error, but the shell removed $1

mawk and BusyBox awk print syntax error with their own prefix and line numbers; the causes and fixes are the same. On Windows cmd.exe, single quotes are not quote characters: run awk from Git Bash or WSL, or put the program in a file with -f.

Sources