in reply to Matching text between line 0 and 4
Like this?
sub reformat { local $_ = shift; print("[\n"); foreach (@_) { chomp; print("{$_}\n"); } print("]\n"); } my @data; my $first = 1; while (<TEST>) { if (/^\w/ && !$first) { reformat(@data); @data = (); } push(@data, $_); $first = 0; } reformat(@data) if @data;
Update: Adapted for new data and output requirements:
my $first = 1; while (<TEST>) { if (/^\w/) { print("\n") if !$first; $first = 0; s/:\s*$//; } else { s/^\s*/ (/; s/\s*$/)/; } print; } print("\n");
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
|
|---|