Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

File parsing question

by mmittiga17 (Scribe)
on Feb 28, 2008 at 19:05 UTC ( [id://670985]=perlquestion: print w/replies, xml ) Need Help??

mmittiga17 has asked for the wisdom of the Perl Monks concerning the following question:

I know this must be easy but I just can not get it write:

I need parse a file that is structured like so:

Start: afadffdafadf stuff stuff stuff stuff Close: xzy Start: afadffdafadf stuff stuff stuff stuff Close: xzy Start: afadffdafadf stuff stuff stuff stuff Close: xzy

For each set of Start and Close I need to get 1 line with Start stuff close on one line. The example above I would then need three lines:

Start: afadffdafadf stuff stuff stuff stuff Close: xzy

Any suggestions or direction will be greatly appreciated

Replies are listed 'Best First'.
Re: File parsing question
by stiller (Friar) on Feb 28, 2008 at 19:37 UTC
    Default, perl treat each line as a record, but you can tell perl to change it's notion of what a record is:
    #!/usr/bin/env perl use strict; use warnings; use English; $INPUT_RECORD_SEPARATOR = "\n\n"; while (<DATA>){ s/\n/ /g; print $_, "\n" } __DATA__ Start: afadffdafadf stuff stuff stuff stuff Close: xzy Start: afadffdafadf stuff stuff stuff stuff Close: xzy Start: afadffdafadf stuff stuff stuff stuff Close: xzy
Re: File parsing question
by Skeeve (Parson) on Feb 28, 2008 at 20:26 UTC

    You can use the range operator ".." to get your sections:

    while (<DATA>) { if ( /^Start:\s/ .. /^Close:\s/ ) { # here you can collect your lines } }

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: File parsing question
by CountZero (Bishop) on Feb 28, 2008 at 21:55 UTC
    And yet another way of doing it:
    use strict; while (<DATA>){ chomp; # Get rid of the EOL print "$_ "; # Don't forget to add a space or the records + will run into each other! print "\n" unless $_; # Print an EOL if the line is empty (=record + separator) } __DATA__ Start: afadffdafadf stuff stuff stuff stuff Close: xzy Start: afadffdafadf stuff stuff stuff stuff Close: xzy Start: afadffdafadf stuff stuff stuff stuff Close: xzy

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: File parsing question
by Popcorn Dave (Abbot) on Feb 28, 2008 at 19:34 UTC
    I may be off base here, but aren't you just looking to remove the carriage return on all but the last line of the file? That should be simple enough to do with a join shouldn't it?


    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

    I would love to change the world, but they won't give me the source code

Re: File parsing question
by mmittiga17 (Scribe) on Feb 29, 2008 at 00:38 UTC
    Thanks Everyone, All of the solutions provided work and I was able to complete my mission today.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://670985]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-23 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found