For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2008 > Help parsing 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 Help parsing a file
juanpo@peoplepc.com

2008-02-11, 6:58 pm

Hi, I'm trying to parse a file that looks like this to create 2
different files based on the first 3 characters

FL1ABC
LG1XYZ
LG2DBA
LG3PYZ
FL1CBA
LG1QWE
LG2ZXC

The first file will have all records starting with FL1 like this:
FL1ABC
FL1CBA

The second file will need to have the FL1 records appended to each of
the LG records below it like this:

FL1ABCLG1XYZ
FL1ABCLG2DBA
FL1ABCLG3PYZ
FL1CBALG1QWE
FL1CBALG2ZXC

Is there a way to easily build this logic using awk?

Thanks in advance for any helps you could provide.

Juan.
Ed Morton

2008-02-11, 6:58 pm



On 2/11/2008 12:59 PM, juanpo@peoplepc.com wrote:
> Hi, I'm trying to parse a file that looks like this to create 2
> different files based on the first 3 characters
>
> FL1ABC
> LG1XYZ
> LG2DBA
> LG3PYZ
> FL1CBA
> LG1QWE
> LG2ZXC
>
> The first file will have all records starting with FL1 like this:
> FL1ABC
> FL1CBA
>
> The second file will need to have the FL1 records appended to each of
> the LG records below it like this:
>
> FL1ABCLG1XYZ
> FL1ABCLG2DBA
> FL1ABCLG3PYZ
> FL1CBALG1QWE
> FL1CBALG2ZXC
>
> Is there a way to easily build this logic using awk?
>
> Thanks in advance for any helps you could provide.
>
> Juan.


This will create a file called "list" of all the FL1* records and print the rest
to stdout:

awk '/^FL1/{key=$0;print > "list";next}{print key $0}' file

Ed.

juanpo@peoplepc.com

2008-02-11, 6:58 pm

On Feb 11, 2:08=A0pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/11/2008 12:59 PM, jua...@peoplepc.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
> This will create a file called "list" of all the FL1* records and print th=

e rest
> to stdout:
>
> awk '/^FL1/{key=3D$0;print > "list";next}{print key $0}' file
>
> =A0 =A0 =A0 =A0 Ed.- Hide quoted text -
>
> - Show quoted text -


Thanks Ed,

That code works very well. I discovered a problem in the original
file I was working from though. Some records in the file don't start
with LG so I need to include a condition to only write LG records to
stdout. The file really looks like this:

FL1ABC
LEX123
LG1XYZ
LG2DBA
LG3PYZ
FL1CBA
LEX999
LG1QWE
LG2ZXC

Using the example above I would only want to write FL1 records along
with LG records, ignoring LEX records.

FL1ABCLG1XYZ
FL1ABCLG2DBA
FL1ABCLG3PYZ
FL1CBALG1QWE
FL1CBALG2ZXC

I tried a couple of things in the code but I just can't get a handle
on the proper syntax to accomplish it.

Thanks again.
Ed Morton

2008-02-11, 6:58 pm



On 2/11/2008 2:14 PM, juanpo@peoplepc.com wrote:
> On Feb 11, 2:08 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> Thanks Ed,
>
> That code works very well. I discovered a problem in the original
> file I was working from though. Some records in the file don't start
> with LG so I need to include a condition to only write LG records to
> stdout. The file really looks like this:
>
> FL1ABC
> LEX123
> LG1XYZ
> LG2DBA
> LG3PYZ
> FL1CBA
> LEX999
> LG1QWE
> LG2ZXC
>
> Using the example above I would only want to write FL1 records along
> with LG records, ignoring LEX records.
>
> FL1ABCLG1XYZ
> FL1ABCLG2DBA
> FL1ABCLG3PYZ
> FL1CBALG1QWE
> FL1CBALG2ZXC
>
> I tried a couple of things in the code but I just can't get a handle
> on the proper syntax to accomplish it.
>
> Thanks again.


awk '/^FL1/{key=$0;print > "list";next} /^LG/{print key $0}' file

Ed.

juanpo@peoplepc.com

2008-02-13, 9:58 pm

On Feb 11, 4:41=A0pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/11/2008 2:14 PM, jua...@peoplepc.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
the rest[color=darkred]
>
>
>
>
>
>
>
>
>
>
>
> awk '/^FL1/{key=3D$0;print > "list";next} /^LG/{print key $0}' file
>
> =A0 =A0 =A0 =A0 Ed.- Hide quoted text -
>
> - Show quoted text -


Thanks Ed, this works great, is there a good awk basics book that you
could recommend?

I appreciate it.
Ed Morton

2008-02-13, 9:58 pm

On 2/13/2008 2:28 PM, juanpo@peoplepc.com wrote:
> On Feb 11, 4:41 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> Thanks Ed, this works great, is there a good awk basics book that you
> could recommend?
>
> I appreciate it.


The only one I ever use, and I still refer to it regularly, is Effective Awk
Programming, Third Edition By Arnold Robbins
(http://www.oreilly.com/catalog/awkprog3/).

I also have the old awk book by Aho, et al.
(http://cm.bell-labs.com/cm/cs/awkbook/) but I never got much out of it so I
couldn't actually recommend it.

Ed.

Sponsored Links







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

Copyright 2008 codecomments.com