Solved

modify text data with additional column

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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…

733 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