Strange awk substr bug

Strange awk substr bug

am 24.01.2008 15:35:38 von vburitica

I think I may have found a bug in awk (see the same behavior in gawk
and nawk), I wanted parse a string and split the date into dashes and
still send certain other fields

info='20071231; 23:56:49; 23:56:49; 0x5be784; 1; 0x210a61; 72089944'

If I do this:

echo $info | \
awk -F'; ' '{print substr($1,1,4) "-" substr($1,5,6) "-"
substr($1,7,9) " " $2 "," $4 "," $6 "," $7}'

I get:

2007-1231-31 23:56:49,0x5be784,0x210a61,72089944

it's odd because I would expect the second substr call to behave like
the first and third, but it's printing out two extra characters. I
wanted 2007-12-31, but it's printing 2007-1231-31 instead.

I fixed this by piping it to cut, but seems pretty ugly to have to do
so.

I'm using ubuntu, just wondering if other *nix are seeing the same

Re: Strange awk substr bug

am 24.01.2008 15:41:19 von Ed Morton

On 1/24/2008 8:35 AM, vburitica@gmail.com wrote:
> I think I may have found a bug in awk (see the same behavior in gawk
> and nawk), I wanted parse a string and split the date into dashes and
> still send certain other fields
>
> info='20071231; 23:56:49; 23:56:49; 0x5be784; 1; 0x210a61; 72089944'
>
> If I do this:
>
> echo $info | \
> awk -F'; ' '{print substr($1,1,4) "-" substr($1,5,6) "-"
> substr($1,7,9) " " $2 "," $4 "," $6 "," $7}'
>
> I get:
>
> 2007-1231-31 23:56:49,0x5be784,0x210a61,72089944

Yes, you would because the 3rd argument to "substr()" is the number of
characters to print, not the character position to end at, so it should be "2"
for the 2nd and 3rd calls to substr() above.

Ed.