bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] [BUG] Diffent outputs from gawk and mawk


From: Assaf Gordon
Subject: Re: [bug-gawk] [BUG] Diffent outputs from gawk and mawk
Date: Mon, 27 Apr 2015 17:11:07 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

Hello Jozef,

On 04/27/2015 02:23 PM, Jozef Zuzelka wrote:
<...>
awk 'length > 127 {print substr ($0, 0, 127)"\t"$2} length < 128 {print 
$1"\t"$2}'
<...>
I think that /mawk/ is the one with correct output, but I may be wrong. I 
attached you printscreen witch outputs.

I think the issue might be in your 'substr()' usage.
awk strings and arrays are 1-based, so the first position should be 
'substr($0,1,127)'.

Using 1 as the first position - gawk and mawk return the same value:

    $ echo "123456789" | gawk '{print substr($0,1,5)}'
    12345
    $ echo "123456789" | mawk '{print substr($0,1,5)}'
    12345

Using 0 as the first position (which I believe is invalid usage), mawk returns 
a string shorter by one character:

    $ echo "123456789" | gawk '{print substr($0,0,5)}'
    12345
    $ echo "123456789" | mawk '{print substr($0,0,5)}'
    1234

Changing 0 to 1 in the your substr example, both return 129 which is correct:
127 character from the substr + 1 tab + 1 newline.

HTH,
 -assaf




reply via email to

[Prev in Thread] Current Thread [Next in Thread]