Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


Thanks for your comments. The error is due to an empty line at the end of the __DATA__section. I must have added it when I pasted the code.

Since the empty line did not match /\d:\d:\d$/, the data for that host was considered to be worth keeping.

I trust the issue is now sorted. I Simply added

next if $a_line =~ /^\s*$/; #ignore empty lines

after reading each line.

Here is the full code, with the extra line check, and the empty __DATA__ line.

use strict; use warnings; use 5.010; my @tmp; my @keep; while (my $a_line = <DATA>) { next if $a_line =~ /^\s*$/; #ignore empty lines chomp $a_line; if ( $a_line =~ /^\d+\s+\D+/ ) { #we have a new host master record #process the data from the previous host process_previous(\@tmp); #on return @tmp may be empty @keep = (@keep, @tmp); @tmp =(); #now we empty it anyway } push @tmp, $a_line; } 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$/ ) { #found a line NOT terminating in 3 digit, each separated b +y a #colon, so we want to keep the whole info abou this host $keep_this_data = 1; last; } } if (!$keep_this_data) { #empty the array @$array_ref = (); } } __DATA__ 7 hostname12 Generic-legacy 10000000AB210ACF6 --- 10000000AB210ACF4 2: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

Kind regards


In reply to Re^2: Regex to extract certain lines only from command output/text file. by arnaud99
in thread Regex to extract certain lines only from command output/text file. by perl514

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (11)
    As of 2018-06-22 16:05 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (124 votes). Check out past polls.