Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK syntax error - what's causing it?

Tags:

bash

awk

I have simple bash script:

#!/bin/sh
column=${1:-1}
awk ' {colawk='$column'+2; print $colawk}'
awk '(x=4; print $x)'

But I have received error:

awk: (x=4; print $x)
awk:     ^ syntax error
awk: cmd. line:1: (x=4; print $x)
awk: cmd. line:1:                ^ unexpected newline or end of string

Why? Code in the previous line works.

like image 910
user710818 Avatar asked May 05 '26 23:05

user710818


2 Answers

An AWK program is a series of pattern action pairs, written as:

condition { action }

where condition is typically an expression and action is a series of commands.

print is not expression but a statement, so it's a syntax error as expected.

like image 189
kev Avatar answered May 08 '26 21:05

kev


Your problem is with using parentheses instead of braces. Try:

awk '{x=4; print $x}'

instead, as in the following transcript:

pax$ echo a b c d e | awk '(x=4; print $x)'
awk: cmd. line:1: (x=4; print $x)
awk: cmd. line:1:     ^ syntax error
awk: cmd. line:2: (x=4; print $x)
awk: cmd. line:2:                ^ unexpected newline or end of string

pax$ echo a b c d e | awk '{x=4; print $x}'
d
like image 25
paxdiablo Avatar answered May 08 '26 20:05

paxdiablo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!