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

I have some date in the form of:
TEST: line 1 line 2 line 3 line 4 TEST2: line 1 ...
what I want to do is take each line that begines with ^\w.* up until, but not including the next ^\w.*
UPDATED
my end goal is to output:
TEST (line 1) (line 2) (line 3) (line 4) TEST1 (line 1) (line 2) (line 3) (line 4)
the actual data is this:
r_acctng: printer is on device 'socket' speed -1 queuing is enabled printing is disabled no entries daemon present r_acctng_8150: printer is on device 'socket' speed -1 queuing is enabled printing is disabled no entries daemon present R_NETADMIN: printer is on device 'socket' speed -1 queuing is enabled printing is disabled no entries daemon present
where I would output:
r_acctng,queueing enabled,printing disabled

Replies are listed 'Best First'.
Re: Matching text between line 0 and 4
by Roy Johnson (Monsignor) on Mar 17, 2005 at 19:50 UTC
    Slurp, split, split each piece:
    undef $/; my $whole_thing = <TEST>; my @chunks = split /(?=^\w)/, $whole_thing; my @chunk_lines = map { [split /\n\s*/] } @chunks;
    or just read line by line, resetting as you go:
    my @chunk_lines; my $this_chunk = 0; while (<TEST>) { if (/^\w/) { ++$this_chunk } else { push @{$chunk_lines[$this_chunk]}, $_ } }

    Caution: Contents may have been coded under pressure.

      It is probably better do do the slurp like this:

      my $whole_thing = do{local $/; <TEST>};

      A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you

      —His Holiness, The Dalai Lama

Re: Matching text between line 0 and 4
by ikegami (Patriarch) on Mar 17, 2005 at 19:56 UTC

    Like this?

    Update: Adapted for new data and output requirements:

    Update 2: Adapted for yet new data and output requirements:

    my $first = 1; while (<TEST>) { if (/^\w/) { if ($first) { $first = 0; } else { print("\n"); } s/:\s*$//; print; } else { print(",queuing $1") if /queuing is (\w+)/; print(",printing $1") if /printing is (\w+)/; } } print("\n"); __END__ output ====== r_acctng,queuing enabled,printing disabled r_acctng_8150,queuing enabled,printing disabled R_NETADMIN,queuing enabled,printing disabled
Re: Matching text between line 0 and 4
by TedPride (Priest) on Mar 17, 2005 at 20:48 UTC
    Simple is best. The following is ugly but offers line by line processing and easily read code.
    use strict; use warnings; while (<DATA>) { print m/(\w+)/, ','; <DATA>; print <DATA> =~ m/ +(.*)/, ','; print <DATA> =~ m/ +(.*)/, "\n"; <DATA>; <DATA>; } __DATA__ r_acctng: printer is on device 'socket' speed -1 queuing is enabled printing is disabled no entries daemon present r_acctng_8150: printer is on device 'socket' speed -1 queuing is enabled printing is disabled no entries daemon present R_NETADMIN: printer is on device 'socket' speed -1 queuing is enabled printing is disabled no entries daemon present