Paul Praet
Paul Praet

Reputation: 1387

awk behaves differently on command-line than from script

I would expect this to put put '1' but instead it outputs '1 2 3'

echo "1 2 3" | awk  "{ print $1 }"

However, when running the awk code from a file it does work (where awkscript.awk contains {print $1}):

echo "1 2 3" | awk -f awkscript.awk

Upvotes: 2

Views: 281

Answers (1)

anubhava
anubhava

Reputation: 784948

You should use:

echo "1 2 3" | awk  '{ print $1 }'

then it prints 1 from command line also. That is ' instead of " in awk command.

Explanation: Using double quote " variable $1 gets expanded beforehand and since that is just an empty string, effectively your command becomes this:

echo "1 2 3" | awk '{ print }'

Upvotes: 3

Related Questions