For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2005 > Obtain field value from section of a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Obtain field value from section of a file
Jonny

2005-04-09, 3:55 pm

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
Michael Heiming

2005-04-09, 3:55 pm

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.
hq00e

2005-04-09, 3:55 pm

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
>



Jonny

2005-04-09, 3:55 pm

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
Loki Harfagr

2005-04-09, 3:55 pm

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 :-)
Jonny

2005-04-09, 8:55 pm

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

Jonny

2005-04-09, 8:55 pm

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

Chris F.A. Johnson

2005-04-09, 8:55 pm

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
Ed Morton

2005-04-09, 8:55 pm



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.
Chris F.A. Johnson

2005-04-10, 3:55 am

On Sat, 09 Apr 2005 at 22:54 GMT, Ed Morton wrote:
>
> Chris F.A. Johnson wrote:
>
[snip][color=darkred]
>
> 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
Jonny

2005-04-10, 8:55 am

Michael Heiming wrote:

> Sounds like homework


Thanks to everyone for replying. It wasn't exactly homework. I've been
trying to teach myself awk, sed and grep in my spare time for a few
months now, and I have found both this group and comp.unix.shell to be
invaluable resources, in addition to the sed-users Yahoo group.

I'm very grateful to everyone who posts solutions here, especially the
regulars who keep it all going (you know who you are).

Maybe some day I will be able to post solutions also. But don't hold
your breath...

Regards,
Jonny
Loki Harfagr

2005-04-10, 3:55 pm

Le Sat, 09 Apr 2005 21:49:53 -0400, Chris F.A. Johnson a écrit_:

> On Sat, 09 Apr 2005 at 22:54 GMT, Ed Morton wrote:
> [snip]
>
> But what if his hair is "Light brown"?


Well, then another trick on separators'd do :
awk 'BEGIN{FS=":";RS=""}/Steve White/{print $5}' employees.txt

Bob Harris

2005-04-10, 3:55 pm

In article <im66e.12044$Uc7.2786@newsfe2-win.ntli.net>,
Jonny <www.mail@ntlworld.com> wrote:

> Michael Heiming wrote:
>
>
> Thanks to everyone for replying. It wasn't exactly homework. I've been
> trying to teach myself awk, sed and grep in my spare time for a few
> months now, and I have found both this group and comp.unix.shell to be
> invaluable resources, in addition to the sed-users Yahoo group.
>
> I'm very grateful to everyone who posts solutions here, especially the
> regulars who keep it all going (you know who you are).
>
> Maybe some day I will be able to post solutions also. But don't hold
> your breath...
>
> Regards,
> Jonny


May I suggest the following web page as a very good quick awk
introduction:

<http://h30097.www3.hp.com/docs/base...B_HTML/ARH9WBTE
/WKXXXXXX.HTM>

It is not too long, but it covers a lot of what awk is and does.

Bob Harris
Ed Morton

2005-04-10, 3:55 pm



Bob Harris wrote:

> In article <im66e.12044$Uc7.2786@newsfe2-win.ntli.net>,
> Jonny <www.mail@ntlworld.com> wrote:
>
>
>
>
> May I suggest the following web page as a very good quick awk
> introduction:
>
> <http://h30097.www3.hp.com/docs/base...B_HTML/ARH9WBTE
> /WKXXXXXX.HTM>
>
> It is not too long, but it covers a lot of what awk is and does.
>
> Bob Harris


Looks like a reasonable general introduction to newer awks. I just
spotted a couple of slight mistakes:

In various spots it uses these:

1) print("found abc\n");
2) printf("%5.2f\n", $2+$3)
3) print("did not find "abc", "qrs", or "xyz"\n");
4) {
i = 1
while(i<=NF) print $i++
}
5) end{
6) while( cmd | getline > 0 )
7) print | "tr '[A-Z]' '[a-z]'"

when it should be:

1) print "found abc"
2) printf "%5.2f\n", $2+$3
3) print("did not find \"abc\", \"qrs\", or \"xyz\""
4) {
i = 1
while(i<=NF) print $(i++)
}
5) END{
6) while( (cmd | getline) > 0 )
7) depends on how you're invoking awk and which version of tr you use
so should be discussed there

Regards,

Ed.


Kenny McCormack

2005-04-10, 8:55 pm

In article <DbmdnZi9gvHly8TfRVn-3A@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>Looks like a reasonable general introduction to newer awks. I just
>spotted a couple of slight mistakes:
>
>In various spots it uses these:
>
> 1) print("found abc\n");
> 2) printf("%5.2f\n", $2+$3)
> 3) print("did not find "abc", "qrs", or "xyz"\n");
> 4) {
> i = 1
> while(i<=NF) print $i++
> }
> 5) end{
> 6) while( cmd | getline > 0 )
> 7) print | "tr '[A-Z]' '[a-z]'"
>
>when it should be:
>
> 1) print "found abc"
> 2) printf "%5.2f\n", $2+$3


These two "come from the same vat". My guess is that in the first, the
intent was to demonstrate printf (i.e., the missed "f" was a typo).

The issue of the use of parentheses in printf in AWK is somewhat more
complex than you make out. I myself always use parens with printf, for the
following reasons:
1) It makes it look more "C-like" (Yes, this can be viewed as
a "goofy" reason)
2) It avoids the ambiguity with the redirect. (This is *not*
a style issue; it is a corner case that happens more often
than you might expect)

Exercise for the reader: Demonstrate that printf is not a function call
(when used with parens) - even though it may look like one (in C, it is).

> 6) while( (cmd | getline) > 0 )


In GAWK (and I think all "modern" AWKs), it works without the extra parens.
Though, you'd be hard pressed to be able to prove it one way or the other
from the documentation.

> 7) depends on how you're invoking awk and which version of tr you use
>so should be discussed there


This is really a shell-quoting issue, having nothing to do with AWK.

Ed Morton

2005-04-10, 8:55 pm



Kenny McCormack wrote:
> In article <DbmdnZi9gvHly8TfRVn-3A@comcast.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
> ...
>
<snip>[color=darkred]
>
>
> These two "come from the same vat". My guess is that in the first, the
> intent was to demonstrate printf (i.e., the missed "f" was a typo).
>
> The issue of the use of parentheses in printf in AWK is somewhat more
> complex than you make out. I myself always use parens with printf, for the
> following reasons:
> 1) It makes it look more "C-like" (Yes, this can be viewed as
> a "goofy" reason)


Glad you caught yourself on that one ;-).

> 2) It avoids the ambiguity with the redirect. (This is *not*
> a style issue; it is a corner case that happens more often
> than you might expect)


I don't know what you're refering to. Care to elaborate?

> Exercise for the reader: Demonstrate that printf is not a function call
> (when used with parens) - even though it may look like one (in C, it is).


PS1> awk 'BEGIN{printf("abc")("def\n")}'
abcdef
PS1> awk 'BEGIN{printf("%s\n"),"abc"}'
abc

Regards,

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com