perl grep implementation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl grep implementation
# 1  
Old 06-01-2011
perl grep implementation

Hi,

I have a huge log file (~5 GB) generated in my server. I have to search through this file for the pattern matching "ERROR" and print only those lines. There is a Perl script available in the server and I have to add a new subroutine so that it will look for this regular expression pattern in an efficient way. I am not supposed to use 'grep' inside perl.

Since I am new to perl, what I have tried so far is:

Code:
#!/usr/bin/perl -w

sub printFun() {
    while(<>) {
       if ($_=~/ERROR/) {
           print $_;
        }
    }
}

printFun ./loghttpd-05212011

But, I am getting an error "Search pattern not terminated at test.pl line 11" when I run this script. Please help me to solve this issue and get the favored output.

Also, please let me know how to optimize this function (in terms of time consumption) in a better way so that it handles this big data file efficiently

Last edited by royalibrahim; 06-01-2011 at 02:39 AM..
# 2  
Old 06-01-2011
First, just passing a filename as a parameter doesn't automatically open it for reading. You'll have to open it yourself. And you have to enclose any string in quotes (for Perl, ./loghttpd-05212011 is very different from "./loghttpd-05212011").

As for optimizing: don't read line-by-line, define the search regex as strict as possible (can "ERROR" occur anywhere in the line? Or at a specific position? Is there any fixed, or well defined, text leading up to it?), and if possible use the built-in grep function.

A simple example:
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

sub printFun {
    my ($file) = @_;
    open my $INPUT, '<', $file or die "Can't open $file for reading: $!\n";
    print grep { /ERROR/ } <$INPUT>;
    close $INPUT;
}

printFun "./loghttpd-05212011";

# 3  
Old 06-01-2011
Quote:
Originally Posted by pludi
First, just passing a filename as a parameter doesn't automatically open it for reading. You'll have to open it yourself. And you have to enclose any string in quotes (for Perl, ./loghttpd-05212011 is very different from "./loghttpd-05212011").

As for optimizing: don't read line-by-line, define the search regex as strict as possible (can "ERROR" occur anywhere in the line? Or at a specific position? Is there any fixed, or well defined, text leading up to it?), and if possible use the built-in grep function.

A simple example:
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

sub printFun {
    my ($file) = @_;
    open my $INPUT, '<', $file or die "Can't open $file for reading: $!\n";
    print grep { /ERROR/ } <$INPUT>;
    close $INPUT;
}

printFun "./loghttpd-05212011";

Thanks a ton Pludi Smilie !! Is that possible to knock off 'grep' keyword in perl and can use something native to perl?

For your questions, yes, the pattern 'ERROR' can occur anywhere in the line. It is like "ERROR: " followed by some error messages.
# 4  
Old 06-01-2011
This grep is native to Perl. Just follow the link I gave for it, it's a native Perl built-in, it doesn't rely on any external tools.
This User Gave Thanks to pludi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep a pattern in perl?

hello Everyone i am a newbie. i have a file which contains the following E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\winclockmtq.lib E:\gtmproj\script\i486_nt\obj\check_geomtools.exe:... (12 Replies)
Discussion started by: Rashid Khan
12 Replies

2. Emergency UNIX and Linux Support

Grep inline use perl

Hi, i have input like this : SS-ID VLAN MAC TIME IP RSSI MODE UAPSD BW GI WMOS DHCP IDENTITY ----- ---- --- ---- -- ---- ---- ----- -- -- ---- ---- -------- 1-1 0 C4:46:19:75:C1:55 23m 192.168.5.253 ... (5 Replies)
Discussion started by: justbow
5 Replies

3. Shell Programming and Scripting

Grep in line use perl

Hi, i want to create perl script to telnet and print the output. : This is the script that i have so far : #!/usr/bin/perl use lib '/usr/lib/perl5/5.14'; use Telnet (); $target = "192.168.5.1"; $user = "root"; $passwd = "admin123"; print... (2 Replies)
Discussion started by: justbow
2 Replies

4. Shell Programming and Scripting

Grep pattern in PERL

Hi, Can anyone tell me how this grep works? Type of input is not known Thanks in advance (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

Grep in PERL

Hi, Can anybody let me know how this grep will work. The input and output is not known. Also can you give me the details of any link where i can find clearly about grep Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

6. Shell Programming and Scripting

Perl + and Grep

Hi All i have this script that uses glob to look in /var/log/messages.* my @messagefiles = glob "/var/log/messages.*"; and the code that uses it is this grep { /NVRM: Xid/ } @messages) but this spits out this /var/log/messages-20111030:Oct 25 13:43:04 brent kernel: NVRM:... (10 Replies)
Discussion started by: ab52
10 Replies

7. Shell Programming and Scripting

grep in perl

Hello I want to grep a line from a file saved in some directory. Can anyone please correct the code below: #!/usr/bin/perl -w $file = "/home/output.txt" $grep_line = "closing zip for topic"; `grep $grep_line* $file`; (1 Reply)
Discussion started by: sureshcisco
1 Replies

8. Shell Programming and Scripting

How to execute Grep in Perl.

$ grep edge test_1 |sort|uniq >result.txt $more result.txt edge-a-pas01.com 10.12.10.11 edge-b-pas02.com 10.12.10.12 edge-c-pas03.com 10.12.10.50 edge-d-pas03.com 10.12.10.10 how do we execute the above grep command using perl? Thanks in advance. (3 Replies)
Discussion started by: sureshcisco
3 Replies

9. Shell Programming and Scripting

Perl grep

OK here's the situation: I have got these lines which I have got to parse. If the line contains a particular string and any element from a previously defined array I need to take that particular line and do some further processing. if ((grep(/$_/,$1)) && (grep($pattern,@myarr))) { #Do... (2 Replies)
Discussion started by: King Nothing
2 Replies

10. Shell Programming and Scripting

grep using Perl

I'm using perl to do a grep of each line in a vendor file and find its occurrences in a specific directory. Any values found is saved in @dir. .....(file opened, etc.) .... while ($line=<FILE>){ @dir = `grep $line * `; } It's the specific usage of the system grep that I'm having... (7 Replies)
Discussion started by: gavineq
7 Replies
Login or Register to Ask a Question