in reply to Regex to extract certain lines only from command output/text file.
Perhaps the following will be helpful:
use strict; use warnings; local $/; my @record; for ( split /(.+Generic.+)/m, <> ) { next unless /\S/; s/\n+$//g; push @record, $_; if ( @record == 2 ) { $record[1] =~ s/(\n?.+?Generic\n)/print $1; ''/ge; print "@record\n" if "$record[0]$record[1]" =~ /---/; undef @record; } }
Output on your data set:
7 hostname12 Generic-legacy 10000000AB210ACF6 --- 10000000AB210ACF4 2:5:4 10000000AB210ACF4 2:3:4 10000000AB210ACF6 3:5:4 20 hostname14 Generic-legacy 10000000AB2A3000C --- 10000000AB2A3000E 3:3:1 21 HOSTNAME Generic 23 srvernam Generic-legacy 5001438002A3004A 3:3:3 5001438002A3004A --- 5001438002A30048 2:3:3 5001438002A30048 2:5:2 5001438002A30048 2:5:2
Usage: perl script.pl inFile [>outFile]
|
---|
In Section
Seekers of Perl Wisdom