parsing text file into array

H

Hamanjam

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. I also posted this in the "awk" forum
because I don't know which would be easier:

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
 
J

John W. Krahn

Hamanjam said:
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. I also posted this in the "awk" forum
because I don't know which would be easier:

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


$ echo "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" | \
perl -ln00e'print for /SlotAddress\W+(\d+)/, /VolumeTag\W+(\w+)/'
4096
DET028L3
4097
DET029L3




John
 
U

usenet

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.

You don't say what you want your output to look like. If you want one
line per "section", tab separated, you could do something like this:

#!/usr/bin/perl
use strict ;
use warnings ;

while (<DATA>) {
print "$1\t" if /^SlotAddress\w+(\d*)/;
print "$1\n" if /^VolumeTag\.*(\w*)/;
}

__DATA__
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

Result:
4096 DET028L3
4097 DET029L3
 
M

Mirco Wahab

Hamanjam said:
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. I also posted this in the "awk" forum
because I don't know which would be easier:

What do you mean with 'parse into array' (subject)
and 'parse into file' (here)?
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

if it's just the cutting out the inner lines, do:

perl -0777pe 's/(?<=\w\n)^.+?\n(?=\w)//gm && s/\.+|\n\n/ /g' hamanjam.txt > new.txt

.... if your data is in hamanjam.txt and
the 'parsed out' stuff gous into 'new.txt'.

But probably you meant something else.

Regards

M.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top