in reply to Regex to extract certain lines only from command output/text file.

G'day perl514,

It seems to me that you only need two short regexes here: qr{^\s*\d+\s} to identify the start of each block of host data and qr{\d:\d:\d\n$} to check the line endings. Here's how I used these in a script:

#!/usr/bin/env perl use strict; use warnings; my $host_start_re = qr{^\s*\d+\s}; my $triple_re = qr{\d:\d:\d\n$}; my @buffered_lines; my $print_buffer = 0; while (<DATA>) { if (/$host_start_re/) { print @buffered_lines if $print_buffer; @buffered_lines = (); $print_buffer = 0; } push @buffered_lines, $_; $print_buffer = 1 unless /$triple_re/; } print @buffered_lines if $print_buffer; __DATA__ 7 hostname12 Generic-legacy 10000000AB210ACF6 --- 10000000AB210ACF4 2:5:4 10000000AB210ACF4 2:3:4 10000000AB210ACF6 3:5:4 9 hostname13 Generic 10000000AB2A3006A 3:5:2 10000000AB2A30068 2:5:2 20 hostname14 Generic-legacy 10000000AB2A3000C --- 10000000AB2A3000E 3:3:1 21 HOSTNAME Generic 22 hsname12 Generic-legacy 10000000ABCDE004A 3:3:3 10000000ABCDE004A 3:5:2 10000000ABCDE0048 2:3:3 23 srvernam Generic-legacy 5001438002A3004A 3:3:3 5001438002A3004A --- 5001438002A30048 2:3:3 5001438002A30048 2:5:2 5001438002A30048 2:5:2

Output:

$ pm_3par_extract.pl 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

-- Ken