LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2012, 08:56 PM   #1
kenji
Member
 
Registered: Sep 2004
Location: Philippines
Distribution: Slackware
Posts: 143

Rep: Reputation: 16
convert grep -vf to perl


Hi,

Good day to all. I just want to ask if you have any idea.

The script that I want to accomplish is, grep a word in a log file from an excluded list which makes "grep -fv exclude.txt log.txt" a perfect choice, but I need to do it in perl because I also have windows servers.

I have been trying to convert it to perl but there is no luck. What I did was, I tried searching and I stumbled upon this link http://www.perlmonks.org/?node_id=138288. After trying the code, the 1st code did not work and the 2nd code works but it only greps the exact line.

What I need to accomplish is, I need to grep a word like "error" or "warning", IP of a computer or a word that is in the exclude file.

Thank you in advance for your help.
 
Old 05-05-2012, 02:19 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I don't do perl these days, but I think this works (I just modified the second one from the link):

Code:
#!/usr/bin/perl
##
## only-in
## find lines which are in the first file, but not in the second.

use warnings;
use strict;

my ($input_file, $exclude_file) = (shift, shift);

die "Usage: $0 INPUT EXCLUDE\n" if ! $exclude_file;

my %exclude;

open (my $exclude_fh, $exclude_file ) ||
    die "Can't read exclude file '$exclude_file': $!\n";
while (defined(my $exclude = <$exclude_fh>)) {
	$exclude{$exclude} = 1;
}
close $exclude_fh;

my $found = 0;
my $line;
open (my $input_fh, $input_file) ||
	die "Can't read input file '$input_file': $!\n";
while (defined(my $input = <$input_fh>)) {
    chomp($input);
    foreach $line (keys %exclude) {
        chomp($line);
        if ( $input =~ /$line/ ) { $found = 1;}
    }
    print "$input\n" unless ($found);
    $found = 0;
}

close $input_fh;

Last edited by kbp; 05-05-2012 at 02:20 AM.
 
Old 05-06-2012, 11:42 PM   #3
kenji
Member
 
Registered: Sep 2004
Location: Philippines
Distribution: Slackware
Posts: 143

Original Poster
Rep: Reputation: 16
Thanks kbp. I can now grep -vf the log files using perl. The script does not work in the raw log (not sure why though) but after grepping' error and warn in the raw log file I can now use the script. Thanks a lot!

Last edited by kenji; 05-06-2012 at 11:44 PM.
 
Old 05-07-2012, 01:31 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED once you have a solution.
 
Old 05-07-2012, 01:38 AM   #5
kenji
Member
 
Registered: Sep 2004
Location: Philippines
Distribution: Slackware
Posts: 143

Original Poster
Rep: Reputation: 16
Thanks grail for reminding. It's been years since I last posted a thread here in LQ.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl grep function casperdaghost Linux - Newbie 7 06-04-2011 08:46 PM
perl grep statement hattori.hanzo Programming 2 11-19-2010 04:01 AM
grep in perl shriyer Linux - Software 2 07-16-2009 01:23 PM
Perl and Grep yah0m Programming 4 03-11-2009 09:26 PM
Need help with grep or perl FredrikN Programming 2 12-16-2001 11:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration