Solved

modify text data with additional column

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

If there are additional columns like time-range; please help in handling that.

Sample text:
      permit      tcp                 10_12_10_0      23      host_10_14_0_181      range      1525      1527     time-range      2Days
 
      permit      tcp      10_12_10_0      23      host_10_14_4_16            range      1525      1527            time-range      30Days

I would need final output as:
scheduler-name 2Days
source 10_12_10_0_23 destination host_10_14_0_181 application tcp_1525_1527

scheduler-name 30Days
source 10_12_10_0_23 destination host_10_14_0_181 application tcp_1525_1527

Other than all existing rules; need one additional line for each time-range value.

Thank you.
0
Comment
Question by:dpk_wal
2 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34957838

sub FlushIt
{
	if ( $timeRange ne '' )
	{
		print "scheduler-name $timeRange\n";
		undef $timeRange;
	}

	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 . (( $maxPort ne '' ) ? ("_" . $maxPort) : '');

	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
		#       permit      tcp      host_10_14_1_50            host_10_13_5_46      range      1531      1550    time-range      2Days

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

		($disposition, $protocol, $source, $destination, $operator, $minPort, $maxPort, $timeRangeLabel, $timeRange) = split(/\s+/);

		$sources{$source} = 1;
		$destinations{$destination} = 1;
		$protocols{$protocol} = 1;
	}
}

FlushIt();

Open in new window


c:\temp>perl foo.pl foo.dat
scheduler-name 2Days
source 10_12_10_0_23 destination host_10_14_0_181 application tcp_1525_1527
scheduler-name 30Days
source 10_12_10_0_23 destination host_10_14_4_16 application tcp_1525_1527

Open in new window


Not sure if there are supposed to be blank lines between the two outputs or not.  Let me know if you need blank line separator.
0
 
LVL 32

Author Closing Comment

by:dpk_wal
ID: 34959897
Thank you; didn't pick up all time-range but am done here for now!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now