Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I have a file containing employee details. For example: John Green D.O.B: 30 March 1965 Hair colour: Black Eye colour: Brown Steve White D.O.B: 21 April 1959 Hair colour: Brown Eye colour: Blue Dave Brown D.O.B: 12 June 1970 Hair colour: Red Eye colour: Brown Please could you tell me how I would obtain Steve White's hair colour? Thanks for your help. Regards, Jonny
Post Follow-up to this messageIn comp.lang.awk Jonny <www.mail@ntlworld.com>: > Hi, > I have a file containing employee details. For example: > John Green > D.O.B: 30 March 1965 > Hair colour: Black > Eye colour: Brown [..] > Please could you tell me how I would obtain Steve White's hair colour? Sounds like homework, have you already tried something out? -- Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94) mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/' #bofh excuse 417: Computer room being moved. Our systems are down for the wend.
Post Follow-up to this messageThere was an almost some case which is about "state machine" in a post
before. Sorry I cannot remember who provided the solution using state
machine.
However, with the same logic and almost the same script you can use this:
gawk "BEGIN{ state=0 } /^Steve/{ state=1 } /^$/{ state=0 } state &&
$0~/Hair colour/"
But, you might prefer this:
gawk -v name="Steve White" "BEGIN{FS="\n";RS=\"\"} $1~name{print $3}"
Sat, 09 Apr 2005 16:18:42 GMT£¬Jonny <www.mail@ntlworld.com> write:
> Hi,
>
> I have a file containing employee details. For example:
>
> John Green
> D.O.B: 30 March 1965
> Hair colour: Black
> Eye colour: Brown
>
> Steve White
> D.O.B: 21 April 1959
> Hair colour: Brown
> Eye colour: Blue
>
> Dave Brown
> D.O.B: 12 June 1970
> Hair colour: Red
> Eye colour: Brown
>
>
> Please could you tell me how I would obtain Steve White's hair colour?
>
> Thanks for your help.
>
> Regards,
> Jonny
>
Post Follow-up to this messageMichael Heiming wrote: > In comp.lang.awk Jonny <www.mail@ntlworld.com>: > > > > [..] > > > Sounds like homework, have you already tried something out? Hi Michael, Before sending my post, I was trying to figure out where to begin, but I really don't know how. I suppose it's back to Google again. Regards, Jonny
Post Follow-up to this messageLe Sat, 09 Apr 2005 16:18:42 +0000, Jonny a écrit_: > Hi, > > I have a file containing employee details. For example: > > John Green > D.O.B: 30 March 1965 > Hair colour: Black > Eye colour: Brown > > Steve White > D.O.B: 21 April 1959 > Hair colour: Brown > Eye colour: Blue > > Dave Brown > D.O.B: 12 June 1970 > Hair colour: Red > Eye colour: Brown > > > Please could you tell me how I would obtain Steve White's hair colour? Hem, dye your hair ?-D) Ok, admitting it's homework I'll give you a clue, think about either a good regexp or better think about the very special RS='' Now, on the bench you go :-) If you devleopped something and are stuck, post it and then we'll see if we can help :-)
Post Follow-up to this messageLoki Harfagr wrote:
> Le Sat, 09 Apr 2005 16:18:42 +0000, Jonny a écrit :
>
>
> Hem, dye your hair ?-D)
>
>
> Ok, admitting it's homework I'll give you a clue, think about
> either a good regexp or better think about the very special
> RS=''
>
> Now, on the bench you go :-)
>
> If you devleopped something and are stuck, post it and
> then we'll see if we can help :-)
This is the best I can do. It works, but please could you tell me how
to do it all in awk.
gawk "BEGIN {FS=\"\n\"; RS=\"\n\n\"}{print $1 \" \" $2 \" \" $3}"
employees.txt|grep "Steve White"|sed "s/.*Hair colour: \(.*\)/\1/"
Thanks for your help.
Regards,
Jonny
Post Follow-up to this messageLoki Harfagr wrote:
> Le Sat, 09 Apr 2005 16:18:42 +0000, Jonny a écrit :
>
>
> Hem, dye your hair ?-D)
>
>
> Ok, admitting it's homework I'll give you a clue, think about
> either a good regexp or better think about the very special
> RS=''
>
> Now, on the bench you go :-)
>
> If you devleopped something and are stuck, post it and
> then we'll see if we can help :-)
Hi Loki,
Thanks for the RS=" clue.
This is the best I can do. It works, but please can you tell me how to
do it all in awk.
gawk "BEGIN {FS=\"\n\"; RS=\"\n\n\"}{print $1 \" \" $2 \" \" $3}"
employees.txt|grep "Steve White"|sed "s/.*Hair colour: \(.*\)/\1/"
Thanks for your help.
Regards,
Jonny
Post Follow-up to this messageOn Sat, 09 Apr 2005 at 19:56 GMT, Jonny wrote:
> Loki Harfagr wrote:
>
>
>
> Hi Loki,
>
> Thanks for the RS=" clue.
>
> This is the best I can do. It works, but please can you tell me how to
> do it all in awk.
>
> gawk "BEGIN {FS=\"\n\"; RS=\"\n\n\"}{print $1 \" \" $2 \" \" $3}"
> employees.txt|grep "Steve White"|sed "s/.*Hair colour: \(.*\)/\1/"
awk 'BEGIN { FS = "\n"; RS = "" }
/Steve White/ { sub(/.*Hair colour: /,"",$3)
print $3
}' employees.txt
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Post Follow-up to this message
Chris F.A. Johnson wrote:
> On Sat, 09 Apr 2005 at 19:56 GMT, Jonny wrote:
>
>
>
> awk 'BEGIN { FS = "\n"; RS = "" }
> /Steve White/ { sub(/.*Hair colour: /,"",$3)
> print $3
> }' employees.txt
>
awk 'BEGIN{RS=""}/Steve White/{print $9}' employees.txt
Ed.
Post Follow-up to this messageOn Sat, 09 Apr 2005 at 22:54 GMT, Ed Morton wrote:
>
> Chris F.A. Johnson wrote:
>
[snip]
>
> awk 'BEGIN{RS=""}/Steve White/{print $9}' employees.txt
But what if his hair is "Light brown"?
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.