For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2004 > print varaible length fields









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 print varaible length fields
dragrid

2004-11-26, 3:55 pm

If you have a log output , how can you print a record header and
subsequent fields , even if the fields that follow each record header
is of variable lengths. ( i guess the hard part for me is to print a
range of fields until i see the next header)
eg
record header name
lucy (field)
charless (field)
ron (field)
record header phone num
111-222-3333
555-111-4444
record header age
34 (field)
45 (field )
21 (field)
all these people live in acton (field)

want to end up with just name and age through out the file.
record header name
lucy
charles
ron
record header age
34
45
21
all these people live in acton (field)

Thanks
Ed Morton

2004-11-26, 3:55 pm



dragrid wrote:

> If you have a log output , how can you print a record header and
> subsequent fields , even if the fields that follow each record header
> is of variable lengths. ( i guess the hard part for me is to print a
> range of fields until i see the next header)
> eg
> record header name
> lucy (field)
> charless (field)
> ron (field)
> record header phone num
> 111-222-3333
> 555-111-4444
> record header age
> 34 (field)
> 45 (field )
> 21 (field)
> all these people live in acton (field)
>
> want to end up with just name and age through out the file.
> record header name
> lucy
> charles
> ron
> record header age
> 34
> 45
> 21
> all these people live in acton (field)
>
> Thanks


Assuming that there really is some pattern represented above by the text
"record header", then you can use that as the Record Separator and a
newline for the field separator, e.g. (untested):

awk -F"\n" -vRS="record header " '$1=="name"||$1=="age"{print RT $0}'

Regards,

Ed.
Sponsored Links







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

Copyright 2008 codecomments.com