in reply to Print lines based on matching words
Your description of the problem makes almost no sense at all and your code makes even less sense. However, maybe what you want is to skip lines containing the word "remark"? If so then:
#!/usr/bin/perl use strict; use warnings; # print the next word followed by object-group while (<DATA>) { print if ! /\bremark\b/; }
should get you started. However, that doesn't jell with the output you are looking for so maybe the access-list lines are key lines containing key words used to extract blocks of data. So lets play with that idea:
#!/usr/bin/perl 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) { next if !exists $groupData{$group}; print $groupData{$group}{line}; print @{$groupData{$group}{data}} if $groupData{$group}{data}; printGroups($groupData{$group}{groups}, %groupData) if exists $groupData{$group}{groups}; } } __DATA__ access-list INSIDE_IN remark Web Users To Web Server access-list INSIDE_IN extended permit tcp object-group WEB-CLIENT obje +ct-group WEB-SERVER object-group WEB_TCP 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 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 WEB-CLIENT group-object PC1_1st group-object PC2_1st object-group network WEB-SERVER network-object host 10.1.1.5 object-group service WEB_TCP tcp port-object eq www port-object eq https 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
prints:
access-list INSIDE_IN extended permit tcp object-group WEB-CLIENT obje +ct-group WEB-SERVER object-group WEB_TCP object-group network WEB-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 WEB-SERVER network-object host 10.1.1.5 object-group service WEB_TCP tcp port-object eq www port-object eq https 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
which looks pretty much like your required output.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Print lines based on matching words
by ArifS (Beadle) on Dec 23, 2014 at 19:01 UTC | |
|
Re^2: Print lines based on matching words
by ArifS (Beadle) on Dec 23, 2014 at 13:53 UTC | |
|
Re^2: Print lines based on matching words
by ArifS (Beadle) on Dec 23, 2014 at 14:32 UTC | |
|
Re^2: Print lines based on matching words
by ArifS (Beadle) on Dec 24, 2014 at 15:25 UTC | |
by AnomalousMonk (Archbishop) on Dec 26, 2014 at 03:12 UTC | |
by ArifS (Beadle) on Dec 26, 2014 at 13:43 UTC | |
|
Re^2: Print lines based on matching words
by ArifS (Beadle) on Jul 07, 2016 at 11:52 UTC | |
|
Re^2: Print lines based on matching words
by ArifS (Beadle) on Jul 11, 2016 at 13:10 UTC | |
by GrandFather (Saint) on Jul 11, 2016 at 21:09 UTC |