For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2007 > parsing text blocks into database









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 parsing text blocks into database
Hamanjam

2007-04-03, 7:01 pm

Hello all. I have a file that has multiple lines set up in blocks
that I need to parse out into a file. Is there an easy to get a file
like the one below parsed out. I only need the Slot Address and
Volume Tag info. I would RTFM, but I don't know which to read or how
to decipher what I want to do:



SlotAddress 4096
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4096
MediaInverted.................No
VolumeTag.....................DET028L3

SlotAddress 4097
SlotState.....................Normal
ASC/ASCQ.......................0000
MediaPresent..................Yes
RobotAccessAllowed...........Yes
SourceElementAddress.........4097
MediaInverted.................No
VolumeTag.....................DET029L3


Thanks in advance.
Jim

Janis Papanagnou

2007-04-03, 7:01 pm

Hamanjam wrote:
> Hello all. I have a file that has multiple lines set up in blocks
> that I need to parse out into a file. Is there an easy to get a file
> like the one below parsed out. I only need the Slot Address and
> Volume Tag info. I would RTFM, but I don't know which to read or how
> to decipher what I want to do:


You didn't mention what output format you want.
With GNU awk (assuming there are no dots in the volume tag)...

BEGIN { FS="[. ]" }
$1 == "SlotAddress" { sa = $2 }
$1 == "VolumeTag" { print sa, $NF }

The output will be...

4096 DET028L3
4097 DET029L3

Adjust the print output format as you need.

Janis

>
>
>
> SlotAddress 4096
> SlotState.....................Normal
> ASC/ASCQ.......................0000
> MediaPresent..................Yes
> RobotAccessAllowed...........Yes
> SourceElementAddress.........4096
> MediaInverted.................No
> VolumeTag.....................DET028L3
>
> SlotAddress 4097
> SlotState.....................Normal
> ASC/ASCQ.......................0000
> MediaPresent..................Yes
> RobotAccessAllowed...........Yes
> SourceElementAddress.........4097
> MediaInverted.................No
> VolumeTag.....................DET029L3
>
>
> Thanks in advance.
> Jim
>

Hamanjam

2007-04-03, 7:01 pm

Thanks Janis!! this is exactly what I needed. I changed the output a
little to have a sql statement so I can drop this into mysql. My
output looks like this now:

insert into slots (slot,volume) values ('4109','DET019L3');
insert into slots (slot,volume) values ('4110','871LLLL3');



My awk script is now:

BEGIN { FS="[. ]" }
$1 == "SlotAddress" { sa = $2 }
$1 == "VolumeTag" { print "insert into slots (slot,volume) values
('"sa"','" $NF "');" }


Sponsored Links







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

Copyright 2008 codecomments.com