Solved

text line with no range or eg

Posted on 2011-02-21
2
295 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

821 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