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: grep for a "`g'"? (gives an "Unmatched `" err) - comp.unix.solaris ...SUBJECT: How to grep for a "`g'"? (gives an "Unmatched `" err) You know how many documents do, uh, "2nd-level:" quoting around words or phrases by ... efficiency in awk - comp.lang.awk... you need more levels of >escaping, things start to look odd, but all can be logically deduced from well >documented axioms. The problem, really, is that awk's behavior ... sed insert - comp.lang.awkOdd that people don't get confused about the ... Sed and AWK are rather closely related in programming ... Sensitivity to criticism * Eccentricity * Behaviour varies ... Content in PDF form fields disappears when tabbed out - comp.text ...Funny thing is, the CA form works fine and shows the content of the fields ... of file - comp.lang.awk Acrobat Creating PDF Forms... how to add ... ... awk behavior ... Difference between SAR and IDIV??? - comp.lang.asm.x86If you don't like the default behavior, you can test the ... is only meaningful when dealing with negative odd ... Bitwise comparison of 2 numbers - comp.lang.awk ... sqlldr not recognizing a file without an extension on Solaris ...... dat; also note that Oracle on Windows can exhibit behaviour ... bit (or even a lot), and I'm sure there are some odd ... for "missing file is a fatal error" - comp.lang.awk ... regsub (and regular expressions in general) trouble. - comp.lang ...... and there's not much you can do to modify its behaviour. ... Unless you've got one of those odd Windows filenames ... match same regex twice? - comp.lang.awk regsub (and ... difference between sun cluster "/dev/did" and "/dev/global ...GB in MB - comp.lang.awk... in late '80 when it became ... environment variables: Observe the difference in behavior ... Odd BACKUP error: unsupported file structure ! - comp ... how to transpose large matrix? - comp.unix.shell... www.seebs.net/log/ <-- lawsuits, religion, and funny ... you, judging from what I could observe from your behaviour ... Awk has *BUILT*IN* support for sparse arrays, >If ... Ligaturers print incorrectly from pdf - comp.text.pdf... day, we could not find an exact reason for this behavior ... Replace block of text - comp.lang.awk Ligaturers print ... my original diagnosis is probably wrong, but it's odd ... convert integer to string - comp.lang.perl.miscAnd count yourself lucky that there are an odd number ... code for about three years, and it has exactly the behavior ... integer to a 16-bit hex number - comp.soft-sys ... awk ... integer overflow in atoi - comp.lang.cYes, the bit of the standard that describes the behavior of strtol which is different ... Since 3 is odd and the powers of 2 are even (VERY even) the result must end in '8'. using cat and << - comp.unix.shell... nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny ... quotes around "$code" and have tried placing a space aftere (^}. ... behaviour of awk ... Converting number to std::string ("itoa()" ) - comp.lang.c++ ...(Because of its defined behavior in case of errors.) |> Or ... never did find a good solution to some of the other odd ... Convert DATE to NUMBER - comp.lang.awk Converting number ... TCP MSS issue - comp.unix.programmerCisco ASA: VPN behaviour when packet loss is high on WAN ... Funny problem with TCP: 1 char per packet - comp ... gensub() question - comp.lang.awk On 13.01.2011 19:06 ... awk sub odd behavior!My awk version:GNU Awk 3.1.5 I have a data file, well formatted, some fields are missing in some lines, but still well formatted. So the output to awk - Application Forum at ObjectMix.comOdd awk behaviour... ... Use this control to limit the display of threads to those newer than the ... 7/14/2012 6:38:40 AM
|