Odd awk behaviour...

  • Follow


I seem to have a strange problem with awk, I can't really work out
what's happening, thought I'd throw it out there and see if anyone can
point me in the right direction!

Thanks
Pete

# grep From test.txt
From: Last, First

# grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
 Last,

# grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
From: Last, First

# grep From test.txt | sed 's/, / /g'
From: Last First
# grep From test.txt | sed 's/, / /g' |  awk '{ print $3 " " $2 }'
 Last

# bash --version
GNU bash, version 3.00.16(1)-release (i586-suse-linux)
Copyright (C) 2004 Free Software Foundation, Inc

Kernel : Linux 2.6.11.4-21.10-default
0
Reply pete 10/7/2008 9:59:14 PM

On 7 Oct, 22:59, pete.broo...@gmail.com wrote:
> I seem to have a strange problem with awk, I can't really work out
> what's happening, thought I'd throw it out there and see if anyone can
> point me in the right direction!
>
> Thanks
> Pete
>
> # grep From test.txt
> From: Last, First
>
> # grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
> =A0Last,
>
> # grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
> From: Last, First
>
> # grep From test.txt | sed 's/, / /g'
> From: Last First
> # grep From test.txt | sed 's/, / /g' | =A0awk '{ print $3 " " $2 }'
> =A0Last
>
> # bash --version
> GNU bash, version 3.00.16(1)-release (i586-suse-linux)
> Copyright (C) 2004 Free Software Foundation, Inc
>
> Kernel : Linux 2.6.11.4-21.10-default

&

# awk --version
GNU Awk 3.1.4
# sed --version
GNU sed version 4.1.4
0
Reply Pete 10/7/2008 10:13:03 PM


On Tue, 7 Oct 2008 14:59:14 -0700 (PDT), pete.brooker@gmail.com wrote:

>I seem to have a strange problem with awk, I can't really work out
>what's happening, thought I'd throw it out there and see if anyone can
>point me in the right direction!

Where's your sample input?  I'm guessing email spool?

Grant.
-- 
http://bugsplatter.id.au/
0
Reply Grant 10/7/2008 10:35:00 PM

On Tue, 7 Oct 2008 15:13:03 -0700 (PDT), Pete <pete.brooker@gmail.com> wrote:

>On 7 Oct, 22:59, pete.broo...@gmail.com wrote:
>> I seem to have a strange problem with awk, I can't really work out
>> what's happening, thought I'd throw it out there and see if anyone can
>> point me in the right direction!
>>
>> Thanks
>> Pete
>>
>> # grep From test.txt
>> From: Last, First
>>
>> # grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
>>  Last,
>>
>> # grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
>> From: Last, First
>>
>> # grep From test.txt | sed 's/, / /g'
>> From: Last First
>> # grep From test.txt | sed 's/, / /g' |  awk '{ print $3 " " $2 }'
>>  Last
>>
>> # bash --version
>> GNU bash, version 3.00.16(1)-release (i586-suse-linux)
>> Copyright (C) 2004 Free Software Foundation, Inc
>>
>> Kernel : Linux 2.6.11.4-21.10-default
>
>&
>
># awk --version
>GNU Awk 3.1.4
># sed --version
>GNU sed version 4.1.4

Okay, sorry about last response, I see it now.

grant@deltree:~$ cat test.txt
From: Last, First
grant@deltree:~$ grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
First Last,

-- $2 does not include the trailing space 

grant@deltree:~$ grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
From: Last, First

grant@deltree:~$ grep From test.txt | sed 's/, / /g'
From: Last First

grant@deltree:~$ grep From test.txt | sed 's/, / /g' |  awk '{ print $3 " " $2 }'
First Last

grant@deltree:~$ bash --version
GNU bash, version 3.2.15(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
grant@deltree:~$ awk --version
GNU Awk 3.1.6
grant@deltree:~$ sed --version
GNU sed version 4.1.5

You need to update your tools, methinks  gawk-3.1.4 and early 3.1.5 
had buglets, as did older bash...

Grant.
-- 
http://bugsplatter.id.au/
0
Reply Grant 10/7/2008 10:44:23 PM

Pete wrote:
> On 7 Oct, 22:59, pete.broo...@gmail.com wrote:
> 
>>I seem to have a strange problem with awk, I can't really work out
>>what's happening, thought I'd throw it out there and see if anyone can
>>point me in the right direction!
>>
>>Thanks
>>Pete
>>
>># grep From test.txt
>>From: Last, First
>>
>># grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
>> Last,

I cannot reproduce this effect with GNU Awk 3.1.5; result is...

First Last,

(The sed won't du what you intend, BTW, since at that point there's
no space after the comma.)

Would the effect be the same if you abandon the grep and sed...?

   awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }' test.txt


Janis

>>
>># grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
>>From: Last, First
>>
>># grep From test.txt | sed 's/, / /g'
>>From: Last First
>># grep From test.txt | sed 's/, / /g' |  awk '{ print $3 " " $2 }'
>> Last
>>
>># bash --version
>>GNU bash, version 3.00.16(1)-release (i586-suse-linux)
>>Copyright (C) 2004 Free Software Foundation, Inc
>>
>>Kernel : Linux 2.6.11.4-21.10-default
> 
> 
> &
> 
> # awk --version
> GNU Awk 3.1.4
> # sed --version
> GNU sed version 4.1.4
0
Reply Janis 10/7/2008 10:44:44 PM

On 7 Oct, 23:44, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
> Pete wrote:
> > On 7 Oct, 22:59, pete.broo...@gmail.com wrote:
>
> >>I seem to have a strange problem with awk, I can't really work out
> >>what's happening, thought I'd throw it out there and see if anyone can
> >>point me in the right direction!
>
> >>Thanks
> >>Pete
>
> >># grep From test.txt
> >>From: Last, First
>
> >># grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
> >> Last,
>
> I cannot reproduce this effect with GNU Awk 3.1.5; result is...
>
> First Last,
>
> (The sed won't du what you intend, BTW, since at that point there's
> no space after the comma.)
>
> Would the effect be the same if you abandon the grep and sed...?
>
> =A0 =A0awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }' test.txt
>
> Janis
>
>
>
> >># grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
> >>From: Last, First
>
> >># grep From test.txt | sed 's/, / /g'
> >>From: Last First
> >># grep From test.txt | sed 's/, / /g' | =A0awk '{ print $3 " " $2 }'
> >> Last
>
> >># bash --version
> >>GNU bash, version 3.00.16(1)-release (i586-suse-linux)
> >>Copyright (C) 2004 Free Software Foundation, Inc
>
> >>Kernel : Linux 2.6.11.4-21.10-default
>
> > &
>
> > # awk --version
> > GNU Awk 3.1.4
> > # sed --version
> > GNU sed version 4.1.4
>
>

# awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }' test.txt
 Last
# /usr/local/bin/awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }'
test.txt
 Last
# awk --version
GNU Awk 3.1.6
# /usr/local/bin/awk --version
GNU Awk 3.1.6
0
Reply Pete 10/8/2008 12:12:52 AM

On 7 Oct, 23:13, Pete <pete.broo...@gmail.com> wrote:
> On 7 Oct, 22:59, pete.broo...@gmail.com wrote:
>
>
>
> > I seem to have a strange problem with awk, I can't really work out
> > what's happening, thought I'd throw it out there and see if anyone can
> > point me in the right direction!
>
> > Thanks
> > Pete
>
> > # grep From test.txt
> > From: Last, First
>
> > # grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
> > =A0Last,
>
> > # grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
> > From: Last, First
>
> > # grep From test.txt | sed 's/, / /g'
> > From: Last First
> > # grep From test.txt | sed 's/, / /g' | =A0awk '{ print $3 " " $2 }'
> > =A0Last
>
> > # bash --version
> > GNU bash, version 3.00.16(1)-release (i586-suse-linux)
> > Copyright (C) 2004 Free Software Foundation, Inc
>
> > Kernel : Linux 2.6.11.4-21.10-default
>
> &
>
> # awk --version
> GNU Awk 3.1.4
> # sed --version
> GNU sed version 4.1.4

Thanks for all the responses, I installed the latest sed & awk and
repeated the other tests and I'm still puzzled :=AC)

# awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }' test.txt
 Last
# /usr/local/bin/awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }'
test.txt
 Last

# awk --version
GNU Awk 3.1.4
# /usr/local/bin/awk --version
GNU Awk 3.1.6

# grep From test.txt | sed 's/, / /g' |  awk '{ print $3 " " $2 }'
 Last
# sed --version
GNU sed version 4.1.4
# awk --version
GNU Awk 3.1.4

# grep From test.txt | /usr/local/bin/sed 's/, / /g' |  /usr/local/bin/
awk '{ print $3 " " $2 }'
 Last
# /usr/local/bin/sed --version
GNU sed version 4.1.5
# /usr/local/bin/awk --version
GNU Awk 3.1.6
0
Reply Pete 10/8/2008 12:13:30 AM

Pete wrote:
> [...]
> 
> Thanks for all the responses, I installed the latest sed & awk and
> repeated the other tests and I'm still puzzled :�)
> 
> # awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }' test.txt
>  Last

What are the results if you replace the print statement by...

   print $3, $2

or...

   printf("%s %s\n", $3, $2)


Janis

> # /usr/local/bin/awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }'
> test.txt
>  Last
> 
> # awk --version
> GNU Awk 3.1.4
> # /usr/local/bin/awk --version
> GNU Awk 3.1.6
> 
> # grep From test.txt | sed 's/, / /g' |  awk '{ print $3 " " $2 }'
>  Last
> # sed --version
> GNU sed version 4.1.4
> # awk --version
> GNU Awk 3.1.4
> 
> # grep From test.txt | /usr/local/bin/sed 's/, / /g' |  /usr/local/bin/
> awk '{ print $3 " " $2 }'
>  Last
> # /usr/local/bin/sed --version
> GNU sed version 4.1.5
> # /usr/local/bin/awk --version
> GNU Awk 3.1.6
0
Reply Janis 10/8/2008 12:47:09 AM

On 8 Oct, 01:47, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
> Pete wrote:
> > [...]
>
> > Thanks for all the responses, I installed the latest sed & awk and
> > repeated the other tests and I'm still puzzled :=AC)
>
> > # awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }' test.txt
> > =A0Last
>
> What are the results if you replace the print statement by...
>
> =A0 =A0print $3, $2
>
> or...
>
> =A0 =A0printf("%s %s\n", $3, $2)
>
> Janis
>
> > # /usr/local/bin/awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }'
> > test.txt
> > =A0Last
>
> > # awk --version
> > GNU Awk 3.1.4
> > # /usr/local/bin/awk --version
> > GNU Awk 3.1.6
>
> > # grep From test.txt | sed 's/, / /g' | =A0awk '{ print $3 " " $2 }'
> > =A0Last
> > # sed --version
> > GNU sed version 4.1.4
> > # awk --version
> > GNU Awk 3.1.4
>
> > # grep From test.txt | /usr/local/bin/sed 's/, / /g' | =A0/usr/local/bi=
n/
> > awk '{ print $3 " " $2 }'
> > =A0Last
> > # /usr/local/bin/sed --version
> > GNU sed version 4.1.5
> > # /usr/local/bin/awk --version
> > GNU Awk 3.1.6
>
>

Weird... the same... *confused* I found some weird reference to LANG
being set and that could influence, but couldn't find anything solid.

*scratches head*

# awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }' test.txt
 Last
# awk '/From/ { sub(/,/,"",$2); print $3, $2 }' test.txt
 Last
# awk '/From/ { sub(/,/,"",$2); printf("%s %s\n", $3, $2)  }' test.txt
 Last
# /usr/local/bin/awk '/From/ { sub(/,/,"",$2); printf("%s %s\n", $3,
$2)  }' test.txt
 Last
# /usr/local/bin/awk '/From/ { sub(/,/,"",$2); print $3, $2 }'
test.txt
 Last
0
Reply Pete 10/8/2008 12:55:20 AM

Pete wrote:
>>
>>[...]
> 
> Weird... the same... *confused* I found some weird reference to LANG
> being set and that could influence, but couldn't find anything solid.

Well, dunno. Have you tried to change LANG or LC_ALL, then...?

   LANG=C awk ' ... '

> 
> *scratches head*
> 
> # awk '/From/ { sub(/,/,"",$2); print $3 " " $2 }' test.txt
>  Last
> # awk '/From/ { sub(/,/,"",$2); print $3, $2 }' test.txt
>  Last
> # awk '/From/ { sub(/,/,"",$2); printf("%s %s\n", $3, $2)  }' test.txt
>  Last
> # /usr/local/bin/awk '/From/ { sub(/,/,"",$2); printf("%s %s\n", $3,
> $2)  }' test.txt
>  Last
> # /usr/local/bin/awk '/From/ { sub(/,/,"",$2); print $3, $2 }'
> test.txt
>  Last

Beyond what I've suggested I'm clueless. What I'd continue to try, if
I'd have observed such strange behaviour, would be to change the test
data for the script to more than 3 fields and individually print each
field to maybe see some correlation. If the strange effect will still
be there I'd switch awk version, then the window/console, and finally
the platform to detect non-tool specific mis-behaviour/-configuration.

Janis
0
Reply Janis 10/8/2008 1:24:57 AM

On 10/7/2008 4:59 PM, pete.brooker@gmail.com wrote:
> I seem to have a strange problem with awk, I can't really work out
> what's happening, thought I'd throw it out there and see if anyone can
> point me in the right direction!
> 
> Thanks
> Pete
> 
> # grep From test.txt
> From: Last, First
> 
> # grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
>  Last,
> 
> # grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
> From: Last, First
> 
> # grep From test.txt | sed 's/, / /g'
> From: Last First
> # grep From test.txt | sed 's/, / /g' |  awk '{ print $3 " " $2 }'
>  Last
> 
> # bash --version
> GNU bash, version 3.00.16(1)-release (i586-suse-linux)
> Copyright (C) 2004 Free Software Foundation, Inc
> 
> Kernel : Linux 2.6.11.4-21.10-default

You appear to have control characters in your input file, probably control-Hs. Try:

awk '/From/{ printf "-----<%s>\n",$0; for (i=1;i<=NF;i++) printf
"%d,%d,<%s>\n",i,length(i),$i }' test.txt | cat -v

to see what awk thinks each field contains.

Regards,

	Ed.

0
Reply Ed 10/8/2008 2:00:05 AM

On 8 Oct, 03:00, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 10/7/2008 4:59 PM, pete.broo...@gmail.com wrote:
>
>
>
> > I seem to have a strange problem with awk, I can't really work out
> > what's happening, thought I'd throw it out there and see if anyone can
> > point me in the right direction!
>
> > Thanks
> > Pete
>
> > # grep From test.txt
> > From: Last, First
>
> > # grep From test.txt | awk '{ print $3 " " $2 }' | sed 's/, //g'
> > =A0Last,
>
> > # grep From test.txt | awk '{ print $1 " " $2 " " $3 }'
> > From: Last, First
>
> > # grep From test.txt | sed 's/, / /g'
> > From: Last First
> > # grep From test.txt | sed 's/, / /g' | =A0awk '{ print $3 " " $2 }'
> > =A0Last
>
> > # bash --version
> > GNU bash, version 3.00.16(1)-release (i586-suse-linux)
> > Copyright (C) 2004 Free Software Foundation, Inc
>
> > Kernel : Linux 2.6.11.4-21.10-default
>
> You appear to have control characters in your input file, probably contro=
l-Hs. Try:
>
> awk '/From/{ printf "-----<%s>\n",$0; for (i=3D1;i<=3DNF;i++) printf
> "%d,%d,<%s>\n",i,length(i),$i }' test.txt | cat -v
>
> to see what awk thinks each field contains.
>
> Regards,
>
> =A0 =A0 =A0 =A0 Ed.

I thought it may have been something like that, but didn't know how to
check... awesome one liner!

Spot on with what the problem was, many thanks. I can stop pulling my
hair out now.

:=AC) *phew*

# awk '/From/{ printf "-----<%s>\n",$0; for (i=3D1;i<=3DNF;i++) printf "%d,
%d,<%s>\n",i,length(i),$i }' test.txt | cat -v
-----<From: Last, First^M>
1,1,<From:>
2,1,<Last,>
3,1,<First^M>
0
Reply Pete 10/8/2008 9:19:57 AM

11 Replies
96 Views

(page loaded in 0.089 seconds)

Similiar Articles:


















7/14/2012 6:38:40 AM


Reply: