in reply to perl / grep help

This is sort of quick and dirty, but it's a start:

my %out; while (<DATA>) { if ( /##head##/ ) { if ( %out ) { print join ',', $out{servername}, @{ $out{serverip} }; print "\n"; } %out = (); } $out{servername} = $1 if /^servername\s+(.*)/; push @{$out{serverip}}, $1 if /^serverip:\s+(.*)/; } __DATA__ ##head## servername abc xxx xxx serverip: 123 xxx ##head## -------- ##head## servername def xxx xxx serverip: 456 serverip: 445566 xxx ##head## ##head## servername ghi xxx xxx serverip: 789 xxx ##head##

Outputs:

abc,123 def,456,445566 ghi,789

Replies are listed 'Best First'.
Re^2: perl / grep help
by WoodyWeaver (Monk) on Jan 08, 2008 at 23:09 UTC
    Rather than a bunch of "if you see this pattern, do this step" kind of things, this would be a good case for dispatch tables.