Solved

Parse text file, look for string, remove other lines, replace string with known names

Posted on 2008-10-14
11
596 Views
Last Modified: 2013-11-18
I am trying to see what the best way to do this. Either perl, batch, cscript or some other command line means.
I need to parse a multi-line text file which contains data like:
---------------------------------------------------------------------------------
Variable = .iso.org.dod.internet.private.enterprises.9.9.392.1.3.21.1.10.10.83.72.65.82.75.45.66.71.65.78.4151
Value    = String 123.123.123.248

Variable = .iso.org.dod.internet.private.enterprises.9.9.392.1.3.21.1.10.10.83.72.65.82.75.45.66.71.65.78.4154
Value    = String 123.123.123.240

End of MIB subtree.
---------------------------------------------------------------------------------

Then I need to only show the results that start with 123.123 so it looks like:
123.123.123.248
123.123.123.240
---------------------------------------------------------------------------------

I then would like to compare it to another source, either a flat file or even a SQL source that would contain a common name for that number and replace the number with the text.

Example:
123.123.123.248 would become                   Host248
123.123.123.240 would become                   Host240
---------------------------------------------------------------------------------
Results file would contain
Host248
Host240
---------------------------------------------------------------------------------
The results find/replace could be up to over 100 so doind a command line replace would be complex.

Thoughts and ideas welcomed.
0
Comment
Question by:edrz01
  • 4
  • 4
  • 3
11 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22715232

##I then would like to compare it to another source,

##either a flat file or even a SQL source

##If you load this into %cn
 

while(<>) {

    next unless /^Value\s*=\s*String\s*(.*)/;

    print "$cn{$1}\n";

}

Open in new window

0
 
LVL 9

Expert Comment

by:gregcmcse
ID: 22715242
This could be done easily in either Perl or VBScript.  Is Perl your preference?
0
 
LVL 9

Expert Comment

by:gregcmcse
ID: 22715723
This should be pretty much what you need:
my %ip_names;
 

open (IN,"myIPtoNAMEfile.txt");
 

	# Following assumes IP address to friendly names file is a CSV 

	# in this format:  123.123.123.240,Host240  [each host on its own line]

while (<IN>) {

	chomp;

	my ( $ip, $friendlyname ) = split(/,/);

	$ip_names{$ip} = $friendlyname;

}

close(IN);
 

open(OUT, "> myoutputfile.txt");
 

open (IN,"mylogfile.txt");

	#Value    = String 123.123.123.240

while (<IN>) {

	chomp;

	if ( /^Value\s*=\s*String\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) {

		print OUT $ip_names{$1} . "\n";

	}

}

close(IN);
 

close(OUT);

Open in new window

0
 
LVL 9

Expert Comment

by:gregcmcse
ID: 22715758
Note, I'd prefer to make sure we're seeing an IP address by validating that we're seeing 1-3 digits, a period, 1-3 digits, a period, 1-3 digits, a period, and 1-3 digits.  I'm assuming there could be other "Value = String" lines in the file.  It gets a little more complicated if there might be other IP addresses in the file that you don't wish to look up that are in the same format:
      Value    = String 123.123.123.240

If there are, let me know and we'll take a different approach.
0
 

Author Comment

by:edrz01
ID: 22721222
Gregcmcse,

I appreciate your help on this. I took your code snippet and created a file called myiptoname.txt file and put my IP's and hostnames seperated by a comma. When I ran the perl script it created a blank myoutputfile.txt - no data.

Also, what if I do have some IP's that have a 1 or 2 digit value for the last octet?
0
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.

 
LVL 39

Expert Comment

by:Adam314
ID: 22723097
Did you have your data in mylogfile.txt, formatted as in your original post?

Post your log file and ip to name file here.
0
 

Author Comment

by:edrz01
ID: 22723172
My source file is called VPNOUT.TXT and looks like this (minus the ------'s):
-----------------------
Variable = .iso.org.dod.internet.private.enterprises.9.9.392.1.3.21.1.10.10.83.72.65.82.75.45.66.71.65.78.4227
123.123.123.248

Variable = .iso.org.dod.internet.private.enterprises.9.9.392.1.3.21.1.10.10.83.72.65.82.75.45.66.71.65.78.4289
123.123.123..240

End of MIB subtree.
--------------------------------------
My myIPtoNAMEfile.txt looks like:
--------------------------------------
123.123.123.240,3P8-5019
123.123.123.248,3P8-5020
123.123.123.88,3S2-5034
123.123.123.96,3S2-5035
--------------------------------------

My (your code) looks like:
my %ip_names;
open (IN,"myIPtoNAMEfile.txt");
      # Following assumes IP address to friendly names file is a CSV
      # in this format:  123.123.123.240,Host240  [each host on its own line]
while (<IN>) {
      chomp;
      my ( $ip, $friendlyname ) = split(/,/);
      $ip_names{$ip} = $friendlyname;
}
close(IN);
open(OUT, "> myoutputfile.txt");
open (IN,"VPNOUT.txt");
      #Value    = String 123.123.123.240
while (<IN>) {
      chomp;
            print  $ip_names{$1};
      if ( /^Value\s*=\s*String\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) {
            print OUT $ip_names{$1} . "\n";
      }
}
close(IN);
close(OUT);
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 22724182
This version also handles it correctly when there is no name for a given IP - it prints "<the_ip> - no name" (where <the_ip> is the ip address).

#!/usr/bin/perl

use strict;

use warnings;
 

my %ip_names;

open (my $in,"<myIPtoNAMEfile.txt") or die "ip2name: $!\n";

while (<$in>) {

	chomp;

	my @f=split/,/;

	next unless $#f>=1;

	$ip_names{$f[0]} = $f[1];

}

close($in);
 
 

open(my $out, ">output.txt") or die "output: $!\n";

open ($in,"VPNOUT.TXT");

while(<$in>) {

	chomp;

	next unless /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;

	if(exists($ip_names{$1})) {print "$ip_names{$1}\n";}

	else {print "$1 - no name\n";}

}

close($in);

close($out);

Open in new window

0
 

Author Closing Comment

by:edrz01
ID: 31506033
Perfect! Thank you so much!
0
 

Author Comment

by:edrz01
ID: 22740818
Adam...
I just realized that this was not updating the external file. When I try to add the print Out code it bombs.
How would I get the output to a file?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22741266
       if(exists($ip_names{$1})) {print $out "$ip_names{$1}\n";}
        else {print $out "$1 - no name\n";}
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

Suggested Solutions

Title # Comments Views Activity
fizzArray2 challenge 1 72
either24  challenge 19 90
WPF - External data binding to controls inside a UserControl 2 54
Problem to page 4 70
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

919 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

21 Experts available now in Live!

Get 1:1 Help Now