mlebel has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I can't seem to figure out what's wrong with my code. I am trying to figure out how get a list of matching lines from a given file, matched from another file.
Here is what I am talking about:
I have a list of ip accounting information from a router that looks like this (TmpIPFile):
Source Destination Packets Bytes 15.254.32.120 10.2.9.2 5 504 79.15.122.235 208.43.3.154 21 2092 79.15.122.235 63.245.217.113 21 2232 79.15.122.235 209.15.236.80 10 1310 79.15.122.235 46.37.179.218 34 4065 63.97.127.34 10.2.9.2 4 471 79.15.122.235 63.141.200.24 19 1811 79.15.122.235 72.251.219.10 437 56713 79.15.122.235 96.7.122.206 215 23318 79.15.122.235 209.200.154.225 77 6257 79.15.122.235 64.94.107.23 13 3436 79.15.122.235 64.74.126.22 23 1527 17.149.36.162 10.2.9.3 14 3416 79.15.122.235 184.25.187.120 49 5772 79.15.122.235 205.251.242.166 21963 32615009 79.15.122.235 12.239.198.71 26 2946 79.15.122.235 184.85.247.120 145 18458 79.15.122.235 184.235.49.15 10 2001 79.15.122.235 207.171.163.162 19 1393 79.31.21.75 10.2.9.2 11 3993 209.68.19.130 10.2.9.2 33 15941 79.15.122.235 64.94.107.16 4 1375 79.15.122.235 207.67.0.233 29 3742 72.247.242.235 10.2.9.2 7 3750 79.15.122.235 64.145.92.232 9 2364 79.15.122.235 208.88.180.89 28 4490 79.15.122.235 94.100.188.227 10 1979 17.149.36.15 10.2.9.3 14 3404 79.15.122.235 128.175.60.118 280 15120 65.54.81.34 10.2.9.2 42 23068 79.15.122.235 209.236.72.16 102 9765 79.15.122.235 65.55.33.50 18 5479 79.15.122.235 17.149.36.197 54 7279 67.148.147.64 10.2.9.2 553 274036 79.15.122.235 204.245.63.99 42 9826 79.15.122.235 207.46.206.74 104 10498 67.148.147.65 10.2.9.3 57 39207 17.172.232.80 10.2.9.3 27 6768 79.15.122.235 217.212.238.134 320 43460 8.8.8.8 10.2.9.6 84 8006 79.15.122.235 74.125.226.176 214 46874 79.15.122.235 23.12.158.224 299 30331 79.15.122.235 68.67.159.207 48 13422 79.15.122.235 208.122.28.12 33 2857 Accounting data age is 0w1d Box#
From this list, I want to be able to pick out specific lines from a list that looks this (TmpLookingForIPFile):
10.2.9.2 10.2.9.3 10.2.9.4 10.2.9.5
And with this list I would like to add up the Bytes Field for each IP and pass that value to another sub for each ip's.
The code that I have thus far for doing the work looks like this:
Please note that I have a ^^M in there, and that is a control character that is found in the original file. I am not 100% sure , but i beleive that chomp; in the previous line would take care of this.#!/usr/bin/perl use warnings; use strict; my $FALSE = 0; my $TRUE = 1; my $Flag = $TRUE; my $number_list = "TmpIPFile"; my $looking_for = "TmpLookingForIPFile"; my $DestDevice = "Box"; my %remember; if ($Flag == $TRUE){ open my $NUMBER_LIST, '<', $number_list or die "$number_list: $!"; while (<$NUMBER_LIST>) { next if /^sh|^\s*Source|^$DestDevice|^Accounting|^^M|^$|/; # +Skip text and empty lines my ($key, $value,) = split; push @{ $remember{$key} }, $value; } close $NUMBER_LIST; open my $LOOKING_FOR, '<', $looking_for or die "$looking_for: $!"; while (<$LOOKING_FOR>) { chomp; for my $value (@{ $remember{$_} }) { print "$_ $value\n"; # Do your calculations here... } } close $LOOKING_FOR; }
Right now, I get nothing on the screen when i run this. I tried all kinds of prints and I cannot figure out what I am doing wrong..
Does anyone know what I can do to make this work?
Please note that this code runs inside another script, hence the $Flag portion of it.
Thanks in advance!
P.S. If you don't fully understand, please ask me questions :-)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Generating a List of numbers
by InfiniteSilence (Curate) on Jan 31, 2012 at 02:50 UTC | |
Re: Generating a List of numbers
by GrandFather (Saint) on Jan 31, 2012 at 02:48 UTC | |
Re: Generating a List of numbers
by JavaFan (Canon) on Jan 31, 2012 at 02:45 UTC | |
Re: Generating a List of numbers
by mlebel (Hermit) on Jan 31, 2012 at 03:29 UTC | |
Re: Generating a List of numbers
by mlebel (Hermit) on Feb 01, 2012 at 03:07 UTC | |
by GrandFather (Saint) on Feb 01, 2012 at 04:16 UTC | |
by mlebel (Hermit) on Feb 01, 2012 at 13:09 UTC | |
by GrandFather (Saint) on Feb 01, 2012 at 20:06 UTC | |
by GrandFather (Saint) on Feb 01, 2012 at 19:56 UTC | |
by aaron_baugher (Curate) on Feb 01, 2012 at 23:45 UTC | |
Re: Generating a List of numbers
by mlebel (Hermit) on Feb 03, 2012 at 00:33 UTC | |
Re: Generating a List of numbers
by mlebel (Hermit) on Feb 02, 2012 at 02:59 UTC | |
by choroba (Cardinal) on Feb 02, 2012 at 09:22 UTC |