parsing text file into array

Discussion in 'Perl Misc' started by Hamanjam, Apr 3, 2007.

  1. Hamanjam

    Hamanjam Guest

    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
    Hamanjam, Apr 3, 2007
    #1
    1. Advertising

  2. 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. 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Apr 3, 2007
    #2
    1. Advertising

  3. Hamanjam

    Guest

    On Apr 3, 9:33 am, "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.


    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


    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    , Apr 3, 2007
    #3
  4. Hamanjam

    Mirco Wahab Guest

    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. 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.
    Mirco Wahab, Apr 3, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott
    Replies:
    1
    Views:
    487
  2. saltedcoffee
    Replies:
    2
    Views:
    542
    Daniel T.
    Apr 5, 2006
  3. Replies:
    5
    Views:
    242
  4. Scott

    Parsing a text file into an array

    Scott, Aug 16, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    123
  5. Domenico Discepola

    Assistance parsing text file using Text::CSV_XS

    Domenico Discepola, Sep 1, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    436
    Domenico Discepola
    Sep 2, 2004
Loading...

Share This Page