edrz01
asked on
Parse text file, look for string, remove other lines, replace string with known names
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.priv ate.enterp rises.9.9. 392.1.3.21 .1.10.10.8 3.72.65.82 .75.45.66. 71.65.78.4 151
Value = String 123.123.123.248
Variable = .iso.org.dod.internet.priv ate.enterp rises.9.9. 392.1.3.21 .1.10.10.8 3.72.65.82 .75.45.66. 71.65.78.4 154
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.
I need to parse a multi-line text file which contains data like:
--------------------------
Variable = .iso.org.dod.internet.priv
Value = String 123.123.123.248
Variable = .iso.org.dod.internet.priv
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.
This could be done easily in either Perl or VBScript. Is Perl your preference?
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);
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.
Value = String 123.123.123.240
If there are, let me know and we'll take a different approach.
ASKER
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?
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?
Did you have your data in mylogfile.txt, formatted as in your original post?
Post your log file and ip to name file here.
Post your log file and ip to name file here.
ASKER
My source file is called VPNOUT.TXT and looks like this (minus the ------'s):
-----------------------
Variable = .iso.org.dod.internet.priv ate.enterp rises.9.9. 392.1.3.21 .1.10.10.8 3.72.65.82 .75.45.66. 71.65.78.4 227
123.123.123.248
Variable = .iso.org.dod.internet.priv ate.enterp rises.9.9. 392.1.3.21 .1.10.10.8 3.72.65.82 .75.45.66. 71.65.78.4 289
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);
-----------------------
Variable = .iso.org.dod.internet.priv
123.123.123.248
Variable = .iso.org.dod.internet.priv
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
print OUT $ip_names{$1} . "\n";
}
}
close(IN);
close(OUT);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect! Thank you so much!
ASKER
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?
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?
if(exists($ip_names{$1})) {print $out "$ip_names{$1}\n";}
else {print $out "$1 - no name\n";}
else {print $out "$1 - no name\n";}
Open in new window