Why does
awk '{print 3 + 3}'
not work, but:
echo 3|awk '{print $1 + 3}'
does?
Couldn't the first statement just output 6? What is the easiest way of printing the result of a calculation?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityAWK statements consist of a pattern and an action: quoting the GNU awk manual
a rule is executed when its pattern matches the current input record
In the first case, you are not providing any input record - so you get no action. In this case, you could use the special pattern BEGIN
awk 'BEGIN {print 3 + 3}'
awk '{print 3 + 3}'
.
Equally well you could do
awk 'END {print 3 + 3}'
awk
script are associated with the BEGIN
pseudo-pattern, the script runs immediately, without looking at its input. With your answer, awk
reads (and ignores/discards) its input until it reaches EOF, and only then executes the statement.
Apr 23, 2016 at 5:54
With bash echo $(( 3 + 3))
or with bc
awk 'BEGIN{print (2+2)}'
<- This?