Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

text line with no range or eg

Posted on 2011-02-21
2
Medium Priority
?
300 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

609 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