Ziegl
Ziegl

Reputation: 147

Strange behaviour of awk

I have encountered the following behaviour of awk, which has me pretty baffled:

echo "" | awk '{print 15}'

outputs

15

BUT:

echo "" | awk '{print 015}'

outputs

13

Replacing 015 with 013, 0013, 0105 and 0130 yields 11, 11, 69 and 88, respectively. Floating point numbers work as expected, with or without leading zeroes.

I have observed this behaviour for GNU Awk versions 3.1.7, 4.0.2 and 4.1.3.

Can anyone make sense of this?

Upvotes: 0

Views: 60

Answers (2)

glenn jackman
glenn jackman

Reputation: 247220

Just a tip: if you want to execute some awk code without needing any input, use the BEGIN block

awk 'BEGIN {
    print 015
    print 15
    print 0x15
}'
13
15
21

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133770

That is expected 015 is being considered as Octal value.

echo "" | awk '{print 015}' ##Octal
13
echo "" | awk '{print 15}'  ##Decimal
15
echo "" | awk '{print 0x15}' ##Hex
21

EDIT: Adding nice link posted by jas in comments too here https://www.gnu.org/software/gawk/manual/html_node/Nondecimal_002dnumbers.html

Upvotes: 2

Related Questions