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

modify text data with additional column

Posted on 2011-02-21
Last Modified: 2012-05-11
Pls refer to my earlier question:

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.
Question by:dpk_wal
LVL 16

Accepted Solution

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 ( <> )

	if ( $_ eq '' ) { FlushIt(); }
		# 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;


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.
LVL 32

Author Closing Comment

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

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

839 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