Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Obtain field value from section of a file
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jonny
04-09-05 08:55 PM


Re: Obtain field value from section of a file
In 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Heiming
04-09-05 08:55 PM


Re: Obtain field value from section of a file
There 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
>



Report this thread to moderator Post Follow-up to this message
Old Post
hq00e
04-09-05 08:55 PM


Re: Obtain field value from section of a file
Michael 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

Report this thread to moderator Post Follow-up to this message
Old Post
Jonny
04-09-05 08:55 PM


Re: Obtain field value from section of a file
Le 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 :-)

Report this thread to moderator Post Follow-up to this message
Old Post
Loki Harfagr
04-09-05 08:55 PM


Re: Obtain field value from section of a file
Loki 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


Report this thread to moderator Post Follow-up to this message
Old Post
Jonny
04-10-05 01:55 AM


Re: Obtain field value from section of a file
Loki 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


Report this thread to moderator Post Follow-up to this message
Old Post
Jonny
04-10-05 01:55 AM


Re: Obtain field value from section of a file
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chris F.A. Johnson
04-10-05 01:55 AM


Re: Obtain field value from section of a file

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-10-05 01:55 AM


Re: Obtain field value from section of a file
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Chris F.A. Johnson
04-10-05 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:12 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.