Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

work on data in text file

Posted on 2011-02-19
7
Medium Priority
?
303 Views
Last Modified: 2012-05-11
Hi,

I have a 30,000 line text file from which I need to compile data.

I will use example,

say I have lines as below:

object-group network C
 network-object host 10.57.31.66
 network-object host 10.57.31.67
 network-object host 10.80.40.201
 network-object host 10.80.40.202
object-group network A
 network-object 10.14.60.0 255.255.252.0
 network-object 10.14.63.0 255.255.255.0
 network-object 10.14.64.0 255.255.255.0
object-group network B
 network-object host 10.13.5.116
 network-object host 10.13.5.117

I need to do following [order of steps is not important; final result is]:

 1. remove all occurance of network-object and object-group network; so the lines remains:
 C
 host 10.57.31.66
 host 10.57.31.67
 host 10.80.40.201
 host 10.80.40.202
 A
 10.14.60.0 255.255.252.0
 10.14.63.0 255.255.255.0
 10.14.64.0 255.255.255.0
 B
 host 10.13.5.116
 host 10.13.5.117
 2. Now transform all subnet masks to slash (/) notation [only output from object A shown below].
 Refer link http://www.mattwaddell.com/2008/08/26/slash-notation-for-subnet-masks-quick-reference/
  10.14.60.0 22
  10.14.63.0 24
  10.14.64.0 24
3. Give name as host_ip or subnet_mask and have lines read as:
host_10_57_31_66     10.57.31.66
host_10_57_31_67     10.57.31.67
host_10_80_40_201    10.80.40.201
host_10_80_40_202    10.80.40.202
10_14_60_0_22           10.14.60.0/22
10_14_63_0_24           10.14.63.0/24
10_14_64_0_24           10.14.64.0/24
host_10_13_5_116     10.13.5.116
host_10_13_5_117     10.13.5.117
4. Finally using host_ip and/or subnet_mask and club with name as below:
host_10_57_31_66     10.57.31.66
host_10_57_31_67     10.57.31.67
host_10_80_40_201    10.80.40.201
host_10_80_40_202    10.80.40.202
10_14_60_0_22           10.14.60.0/22
10_14_63_0_24           10.14.63.0/24
10_14_64_0_24           10.14.64.0/24
host_10_13_5_116     10.13.5.116
host_10_13_5_117     10.13.5.117

set C address host_10_57_31_66
set C address host_10_57_31_67
set C address host_10_80_40_201
set C address host_10_80_40_202
set A address 10_14_60_0_22       
set A address 10_14_63_0_24       
set A address 10_14_64_0_24       
set B address host_10_13_5_116
set B address host_10_13_5_117

Sorry for the long question.

Thank you for all the help.
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
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34932627
Do you still need help with this?  It seems to overlap with your other project (:modify/sort data in text file").
0
 
LVL 32

Author Comment

by:dpk_wal
ID: 34935440
Yes, this overlaps only in the final part where I get address set with addresses.

Am expecting a new text file with possible more lines and hence requesting complete solution than just piece solutions.

This time I have use excel and text editors to replace/sort data; want an easier approach as it looks this effort would get replicated.

Thank you.
0
 
LVL 32

Author Comment

by:dpk_wal
ID: 34948775
Any updates on this project.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 total points
ID: 34953301
sub FlushIt()
{
	foreach $host (@hostNames)
	{
		if ( ! ( $host =~ /^host/ ) ) #  10.14.60.0 255.255.252.0
		{
			($ip,$mask) = split(/\s/,$host);
			($m1,$m2,$m3,$m4) = split(/\./,$mask);

			# transform subnet mask to slash (/) notation
			if    (( $m3 ==  240 ) && ( $m4 == 0  )) { $slashNum = 20; }
			elsif (( $m3 ==  248 ) && ( $m4 == 0  )) { $slashNum = 21; }
			elsif (( $m3 ==  252 ) && ( $m4 == 0  )) { $slashNum = 22; }
			elsif (( $m3 ==  254 ) && ( $m4 == 0  )) { $slashNum = 23; }
			elsif (( $m3 ==  255 ) && ( $m4 == 0  )) { $slashNum = 24; }
			elsif (( $m3 ==  255 ) && ( $m4 == 128)) { $slashNum = 25; }
			elsif (( $m3 ==  255 ) && ( $m4 == 192)) { $slashNum = 26; }
			elsif (( $m3 ==  255 ) && ( $m4 == 224)) { $slashNum = 27; }
			elsif (( $m3 ==  255 ) && ( $m4 == 240)) { $slashNum = 28; }
			elsif (( $m3 ==  255 ) && ( $m4 == 248)) { $slashNum = 29; }
			elsif (( $m3 ==  255 ) && ( $m4 == 252)) { $slashNum = 30; }

			# 10_14_63_0_24           10.14.63.0/24

			$ip2 = $ip;
			$ip2 =~ s/\./\_/g;

			$host = "$ip2\_$slashNum";
			print "$host\t$ip/$slashNum\n";


		}
		else		# host 10.80.40.201
		{
			# host_10_80_40_201    10.80.40.201

			$host =~ s/host\s+//; 
			$ip = $host;
			$host =~ s/\./\_/g; 
			$host = "host_$host";
			print "$host\t$ip\n";

		}

		push(@setCommands, "set $groupName address $host\n");
	}

	undef @hostNames;
}

while ( <> )
{
	s/[\r\n]//g;
	if ( /object-group network\s+/ ) { FlushIt(); $groupName = $'; }
	elsif ( /network-object\s+/ ) { push(@hostNames, $'); }
}
FlushIt();

print "\n";
print @setCommands;

Open in new window


object-group network C
 network-object host 10.57.31.66
 network-object host 10.57.31.67
 network-object host 10.80.40.201
 network-object host 10.80.40.202
object-group network A
 network-object 10.14.60.0 255.255.252.0
 network-object 10.14.63.0 255.255.255.0
 network-object 10.14.64.0 255.255.255.0
object-group network B
 network-object host 10.13.5.116
 network-object host 10.

Open in new window

c:\temp>perl foo.pl foo.txt
host_10_57_31_66        10.57.31.66
host_10_57_31_67        10.57.31.67
host_10_80_40_201       10.80.40.201
host_10_80_40_202       10.80.40.202
10_14_60_0_22   10.14.60.0/22
10_14_63_0_24   10.14.63.0/24
10_14_64_0_24   10.14.64.0/24
host_10_13_5_116        10.13.5.116
host_10_13_5_117        10.13.5.117

set C address host_10_57_31_66
set C address host_10_57_31_67
set C address host_10_80_40_201
set C address host_10_80_40_202
set A address 10_14_60_0_22
set A address 10_14_63_0_24
set A address 10_14_64_0_24
set B address host_10_13_5_116
set B address host_10_13_5_117

c:\temp>

Open in new window

0
 
LVL 32

Author Comment

by:dpk_wal
ID: 34957676
Thank you so much for working on this.
Am asking a favor here; can you also please look at two of my related questions to an earlier solution given by you.

Regards.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34957733

Post the links here and I will take a look.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

688 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