I am trying to skip a Section if it already Exists (above). However, the following script add a new line "Exists", but doesn't skip the Section.
use strict; use warnings; use 5.010; my @lists; my %group; my $lastGroup; while (defined(my $line = <DATA>)) { next if $line =~ /^access-list .*\bremark\b/; given ($line) { when (/^access-list/) { my @keys = $line =~ /([A-Z]+[-_][A-Z]+)/g; push @lists, {groups => \@keys, line => $line}; } when (/object-group\s+(\S+)\s+(\S+)/) { $group{$2}{line} = $line; $group{$2}{data} = []; $lastGroup = $2; } default { push @{$group{$lastGroup}{data}}, $line; push @{$group{$lastGroup}{groups}}, $1 if $line =~ /^\s+\S+\s+(\S+)$/; } } } for my $list (@lists) { print $list->{line}; printGroups($list->{groups}, %group); print "\n"; } sub printGroups { my ($groups, %groupData) = @_; for my $group (sort @$groups) { ############ Skip if Exists above ##### print "Exists above\n" if exists $groupData{$group}; ############ end Skip ##### next if !exists $groupData{$group}; print $groupData{$group}{line}; print @{$groupData{$group}{data}} if $groupData{$group}{data}; printGroups($groupData{$group}{groups}, %groupData) if Existss $groupData{$group}{groups}; } } __DATA__ access-list INSIDE_IN remark EMAIL To EMAIL Server access-list INSIDE_IN extended permit tcp object-group EMAIL-CLIENT ob +ject-group EMAIL-SERVER object-group SMTP_TCP access-list INSIDE_IN extended permit udp object-group EMAIL-CLIENT ob +ject-group EMAIL-SERVER object-group SMTP_UDP object-group network PC1_1st network-object host 10.1.1.11 object-group network PC2_1st network-object host 10.1.1.12 object-group network EMAIL-CLIENT group-object PC1_1st group-object PC2_1st object-group network EMAIL-SERVER network-object host 10.1.1.6 object-group service SMTP_TCP tcp port-object eq SMTP object-group service SMTP_UDP udp port-object eq SMTP
Current OUTPUT
access-list INSIDE_IN extended permit tcp object-group EMAIL-CLIENT ob +ject-group EMAIL-SERVER object-group SMTP_TCP Exists above object-group network EMAIL-CLIENT group-object PC1_1st group-object PC2_1st Exists above object-group network PC1_1st network-object host 10.1.1.11 Exists above object-group network PC2_1st network-object host 10.1.1.12 Exists above object-group network EMAIL-SERVER network-object host 10.1.1.6 Exists above object-group service SMTP_TCP tcp port-object eq SMTP access-list INSIDE_IN extended permit udp object-group EMAIL-CLIENT ob +ject-group EMAIL-SERVER object-group SMTP_UDP Exists above object-group network EMAIL-CLIENT group-object PC1_1st group-object PC2_1st Exists above object-group network PC1_1st network-object host 10.1.1.11 Exists above object-group network PC2_1st network-object host 10.1.1.12 Exists above object-group network EMAIL-SERVER network-object host 10.1.1.6 Exists above object-group service SMTP_UDP udp port-object eq SMTP
OUTPUT needed
access-list INSIDE_IN extended permit tcp object-group EMAIL-CLIENT ob +ject-group EMAIL-SERVER object-group SMTP_TCP object-group network EMAIL-CLIENT group-object PC1_1st group-object PC2_1st object-group network PC1_1st network-object host 10.1.1.11 object-group network PC2_1st network-object host 10.1.1.12 object-group network EMAIL-SERVER network-object host 10.1.1.6 object-group service SMTP_TCP tcp port-object eq SMTP access-list INSIDE_IN extended permit udp object-group EMAIL-CLIENT ob +ject-group EMAIL-SERVER object-group SMTP_UDP Exists above Exists above Exists above Exists above object-group service SMTP_UDP udp port-object eq SMTP
Please let me know.

In reply to Re^2: Print lines based on matching words by ArifS
in thread Print lines based on matching words by ArifS

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.