Hello
How can I read a txt file with IP-addresses and grep these IP-addesses from several logfiles and print these result to a file?.
I know that these IP-addresses in IP.TXT exist in the logfiles.
But I want to know which logfile contain these IP-addresses.
I have a file with several IP-addresses like this.
IP.TXT
192.168.1.1
10.10.10.10
and so on
Then I have several logfiles that contain these IP-addresses like this.
FILE1_LOG
1.1.1.1
192.168.1.1
10.10.10.10
FILE2_LOG
2.2.2.2
10.10.11.11
10.10.10.10
and so on
The result I want is like this.
Resultfile.txt
IP 192.168.1.1 is in file FILE1_LOG.
IP 10.10.10.10 is in file FILE2_LOG.
The code so far
(I can read the lines from the logfiles, but dont know how to read in the IP.TXT and grep these).
use strict;
my $logdir=".";
opendir(LOGS, $logdir) or die ("Cant open $logdir");
my $pattern = '_log';
my @logfiles = grep /$pattern/,(readdir LOGS);
closedir(LOGS);
foreach my $file (sort @logfiles) {
my $filename = $logdir . "/" . $file;
open(LOG, $filename);
my @line = <LOG>;
if (my @hit = grep /I WANT TO GREP FROM IP.TXT/, @line) {
chomp(@hit);
print ("IP @hit is in file $file\n");
}
}
Or can I do it another way?
//Anders Andersson
janitored by ybiC: <code> tags
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.