3

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.