Hello Sirs - please take it easy on me if this is an elementary question, I'm fairly new to Perl. I could not find any relevant topics.

I am trying to define a string from a subset of each line which matches my regex, and then read down in the file to find the other lines which match the string. If there is more than one match, print the number of matches and the lines.

My data:

src=2.2.2.2 dst=1.1.1.1 src_port=50232 dst_port=514 reason=AGE OUT src=2.2.2.2 dst=1.1.1.1 src_port=50232 dst_port=514 reason=Traffic Den +ied src=3.3.3.3 dst=4.4.4.4 src_port=50235 dst_port=123 reason=AGE OUT src=3.3.3.3 dst=4.4.4.4 src_port=50235 dst_port=123 reason=AGE OUT

My attempt seems to only print the first match. Is that because the inside file loop passes EOF to the outside? I believe I have used this before with arrays and it worked, but this file is too large to take into memory.

#!/usr/bin/perl use strict; use warnings; my $file = 'tmpfile'; my $match; my $numelements; open my $info, $file or die "Could not open $file: $!"; while( my $line = <$info>) { if ( $line =~ m/(src\=\d+\.\d+\.\d+\.\d+\sdst\=\d+\.\d+\.\d+\. +\d+\ssrc\_port\=\d+\sdst\_port\=\d+).*AGE OUT/ ) { $match = "$1"; push (my @dups, $line); while( my $twoline = <$info> ) { if ( $twoline =~ /$match/ ) { push @dups, $twoline; } } $numelements = @dups; print "$match has $numelements elements\n"; if ( $numelements > 1 ) { print join("\n", @dups); } } } close $info;

Output I get:

$./tcprst.pl src=2.2.2.2 dst=1.1.1.1 src_port=50232 dst_port=514 has 2 elements src=2.2.2.2 dst=1.1.1.1 src_port=50232 dst_port=514 reason=AGE OUT src=2.2.2.2 dst=1.1.1.1 src_port=50232 dst_port=514 reason=Traffic Den +ied $

Output I expect:

src=2.2.2.2 dst=1.1.1.1 src_port=50232 dst_port=514 has 2 elements src=2.2.2.2 dst=1.1.1.1 src_port=50232 dst_port=514 reason=AGE OUT src=2.2.2.2 dst=1.1.1.1 src_port=50232 dst_port=514 reason=Traffic Den +ied src=3.3.3.3 dst=4.4.4.4 src_port=50235 dst_port=123 has 2 elements src=3.3.3.3 dst=4.4.4.4 src_port=50235 dst_port=123 reason=AGE OUT src=3.3.3.3 dst=4.4.4.4 src_port=50235 dst_port=123 reason=AGE OUT

Thank you in advance


In reply to Define string on current line, then match other lines with string below the line by Mashed Potato

Title:
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, details, 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, summary, 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.