Solved

modify text data with additional column

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now