Solved

text line with no range or eg

Posted on 2011-02-21
2
296 Views
Last Modified: 2012-05-11
Pls refer to my earlier question:
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_26836792.html

I have lines which do not have range or eq as listed in question earlier.

Sample text:
permit      tcp      host_10_13_20_62      host_10_14_6_84      port_30094
permit      tcp      host_10_13_35_51      host_10_14_6_84      port_30094

permit      tcp      10_96_72_64      29      host_10_14_5_53      Netbackup1      

permit      tcp      host_10_96_72_64       10_14_5_53 30      Netbackup1      

permit      tcp      10_13_35_51      30  10_14_6_84 22       port_30094

Here again I can have combination of 10_ and host_ addresses for source and destination. All 10_ addresses have a single/two digit mask separated by space or tab.
For application I need the last column.

Expected output:
source [ host_10_13_20_62 host_10_13_35_51 ] destination host_10_14_6_84 application port_30094

source 10_96_72_64_29 destination host_10_14_5_53 application Netbackup1

source host_10_96_72_64 destination 10_14_5_53_30 application Netbackup1

source 10_13_35_51_30 destination 10_14_6_84_22 application port_30094

Thank you.
0
Comment
Question by:dpk_wal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34957794
This should handle both cases with and without a range

sub FlushIt
{
	my @sources = sort(keys(%sources));
	my $sourceCount = @sources;
	my $sourceString = ($sourceCount > 1 ) ? ( "[ " . join(" ",@sources) . " ]" ) : $sources[0];

	my @destinations = sort(keys(%destinations));
	my $destinationCount = @destinations;
	my $destinationString = ($destinationCount > 1 ) ? ( "[ " . join(" ",@destinations) . " ]" ) : $destinations[0];

	my @protocols = sort(keys(%protocols));
	my $protocolCount = @protocols;
	my $protocolString = join("_",@protocols) . (( $minPort ne '' ) ? ("_" . $minPort) : '') . (( $maxPort ne '' ) ? ("_" . $maxPort) : '');

	if ( $sourceString ne '' )
	{
		print "source $sourceString destination $destinationString application $protocolString\n";
	}

	undef %sources;
	undef %destinations;
	undef %protocols;
}


while ( <> )
{
	s/[\r\n]//g;

	if ( $_ eq '' ) { FlushIt(); }
	else
	{
		# All IP subnets starting with 10_ and followed by two digit mask;
		# should get listed as subnet_mask in the final output.
		# Eg, in text above, 10_12_10_0      23 should get listed as 10_12_10_0_23

		while ( s/(\s+10\_[0-9\_]+)\s+([0-9]+)(\s+)/$1\_$2$3/ ) {}

		#       permit      tcp      host_10_14_1_50            host_10_13_5_46      range      1531      1550

		s/^\s+//;		# trim leading spaces
		s/\s+$//;		# trim trailing spaces

		($disposition, $protocol, $source, $destination, $operator, $minPort, $maxPort) = split(/\s+/);
		##print STDERR join("\n", $disposition, $protocol, $source, $destination, $operator, $minPort, $maxPort) . "\n\n";

		$sources{$source} = 1;
		$destinations{$destination} = 1;
		if ( $operator eq 'range' ) { $protocols{$protocol} = 1; }
		else { $protocols{$operator} = 1; }
	}
}

FlushIt();

Open in new window


c:\temp>perl foo.pl foo.dat
source [ host_10_13_20_62 host_10_13_35_51 ] destination host_10_14_6_84 application port_30094
source 10_96_72_64_29 destination host_10_14_5_53 application Netbackup1
source host_10_96_72_64 destination 10_14_5_53_30 application Netbackup1
source 10_13_35_51_30 destination 10_14_6_84_22 application port_30094

Open in new window

0
 
LVL 32

Author Closing Comment

by:dpk_wal
ID: 34958013
Works great; thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question