Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Regex to extract certain lines only from command output/text file.

by kcott (Canon)
on Mar 08, 2013 at 12:38 UTC ( #1022414=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022414]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2016-10-01 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (3 votes). Check out past polls.