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 arnaud99 (Beadle)
on Mar 07, 2013 at 19:22 UTC ( #1022295=note: print w/ replies, xml ) Need Help??


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

Hi

I have taken another approach to what I think you are trying to do. Hopefully you can use this to achieve what you want.

use strict; use warnings; my @tmp; my @keep; my $first_time = 1; while (my $a_line = <DATA>) { chomp $a_line; push @tmp, $a_line; if ( $a_line =~ /^\d+\s+\D+/ ) { if ($first_time) { $first_time = 0; } else { #we have a new host master record #process the data from the previous host process_previous(\@tmp); @keep = (@keep, @tmp); @tmp =(); } } } process_previous(\@tmp); @keep = (@keep, @tmp); @tmp=(); print "$_\n" for @keep; exit(0); #----------------- SUBS ---------------------------- sub process_previous { my $array_ref = shift; my $keep_this_data = 0; foreach my $elem(@$array_ref) { if ($elem !~ /\d:\d:\d$/ ) { $keep_this_data = 1; last; } } if (!$keep_this_data) { #empty the array @$array_ref = (); } } __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 23 srvernam Generic-legacy 5001438002A3004A 3:3:3 5001438002A3004A --- 5001438002A30048 2:3:3 5001438002A30048 2:5:2 5001438002A30048 2:5:2 9 hostname13 Generic 10000000AB2A3006A 3:5:2 10000000AB2A30068 2:5:2 21 HOSTNAME Generic 9 hostname13 Generic 10000000AB2A3006A 3:5:2 10000000AB2A30068 2:5:2

output

7 hostname12 Generic-legacy 10000000AB210ACF6 --- 10000000AB210ACF4 2:5:4 10000000AB210ACF4 2:3:4 10000000AB210ACF6 3:5:4 23 srvernam Generic-legacy 5001438002A3004A 3:3:3 5001438002A3004A --- 5001438002A30048 2:3:3 5001438002A30048 2:5:2 5001438002A30048 2:5:2 21 HOSTNAME Generic

I hope this helps.

Arnaud


Comment on Re: Regex to extract certain lines only from command output/text file.
Select or Download Code
Re^2: Regex to extract certain lines only from command output/text file.
by Anonymous Monk on Mar 08, 2013 at 00:55 UTC

    arnaud99
    Check your script, the output indicated in your post is really different to what your code really shows. Below is the output of your script:

    7 hostname12 Generic-legacy 10000000AB210ACF6 --- 10000000AB210ACF4 2:5:4 10000000AB210ACF4 2:3:4 10000000AB210ACF6 3:5:4 9 hostname13 Generic 10000000AB2A3006A 3:5:2 5001438002A3004A --- 5001438002A30048 2:3:3 5001438002A30048 2:5:2 5001438002A30048 2:5:2 9 hostname13 Generic 10000000AB2A3006A 3:5:2 10000000AB2A30068 2:5:2 21 HOSTNAME Generic
    According, to the OP,
    9 hostname13 Generic 10000000AB2A3006A 3:5:2 10000000AB2A30068 2:5:2
    Is not suppose to be shown in the final output. Your script even modified some info. like this:
    9 hostname13 Generic 10000000AB2A3006A 3:5:2 5001438002A3004A --- 5001438002A30048 2:3:3 5001438002A30048 2:5:2 5001438002A30048 2:5:2 9 hostname13 Generic 10000000AB2A3006A 3:5:2 10000000AB2A30068 2:5:2
    The OP has only one "9 hostname13 Generic", so how did it become two, in the final output.

      Hi,

      In response to: The OP has only one "9 hostname13 Generic", so how did it become two, in the final output.

      I added an extra one in the __DATA__ section, for testing purposes. Only one was showing in the output (the last one in __DATA__ , due to an empty line at the end of the same __DATA__ section.

      (The fix is also posted in one of the replies)

      Thanks.

      Arnaud

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (15)
As of 2015-07-06 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (74 votes), past polls