Forum Moderators: coopster & phranque

Message Too Old, No Replies

Script required

want a ready script for grep

         

perl12

11:54 am on Jan 28, 2004 (gmt 0)

10+ Year Member


Hi,
I am very new to perl. I want to know if there is any ready script for looking for the word "error" in a particular log file. Pls help me if somebody knows a ready script. I know we have to grep for the word "error". But i am not able to put it in a neat and workable form. Pls somebody help

waiting for response,
Perl12

SeanW

2:42 pm on Jan 28, 2004 (gmt 0)

10+ Year Member



What's wrong with grep? ;)

If you want to do it in perl,

[perl]
while <STDIN> {
print if /\berror\b/;
}
[/perl]

That'll act as a filter, ie

cat logfile ¦ myscript.pl

Sean

perl12

5:23 am on Jan 29, 2004 (gmt 0)

10+ Year Member



Sean,
Thanks a lot . But my problem is that there are around 200 logfiles on my system & i want to run a single script to find "errors¦warnings" in them. The script that you have sent is throwing an error "syntax error at .//myscript.pl line 3, near "while <STDIN>"
Execution of .//myscript.pl aborted due to compilation errors."
Pls help

dkubb

6:05 am on Jan 29, 2004 (gmt 0)

10+ Year Member



I'd probably just use find and grep for this. find can identify each of the files, and pipe them into xargs will execute a command fo each filename.

If all the log files are in the same directory, go to the directory and type in:

find . -type f -print0 ¦ xargs -0 grep -E 'error¦warning'

Since this is a perl forum, here's a way to do it with find and perl:

find . -type f -print0 ¦ xargs -0 perl -ne 'print if /error¦warning/'

perl12

6:59 am on Jan 29, 2004 (gmt 0)

10+ Year Member



hi dkubb,
Thanks a million. The find & perl works . But it works only when I type the coomand in the directory where all log files are kept --find . -type f -print ¦ xargs perl -ne 'print if /error¦warning/'.

If i try to make a script of it i get this message ----syntax error at .//myscript.pl line 3, near "-ne"
Execution of .//myscript.pl aborted due to compilation errors.

My script is simple & it goes loke this

#! /usr/atria/bin/Perl

find . -type f -print ¦ xargs perl -ne 'print if /error¦warning/'

pls do let me know why I am not able to put in in the script form

waiting for response..
perl12

tombola

10:05 am on Jan 29, 2004 (gmt 0)

10+ Year Member



One of the options to invoke unix commands in a Perl script is to use backticks (backquotes):

#! /usr/atria/bin/Perl

`find . -type f -print ¦ xargs perl -ne 'print if /error¦warning/'`;

perl12

10:21 am on Jan 29, 2004 (gmt 0)

10+ Year Member



tambola,
If I use backquotes it just doe'nt work ...i mean nothing happens...it just comes to the command promt doing nothing.....pls help

tombola

10:53 am on Jan 29, 2004 (gmt 0)

10+ Year Member



Sorry, forgot to use a variable:

$error = `find . -type f -print ¦ xargs perl -ne 'print if /error¦warning/'`;

This should work.

perl12

11:16 am on Jan 29, 2004 (gmt 0)

10+ Year Member



no luck tambola.....it just returns to the command prompt again without doing anything...i dont understand y this is happening...the find & perl command works by itself but when tried to put in a script it fails.....pls pls pls help me

perl12

11:27 am on Jan 29, 2004 (gmt 0)

10+ Year Member



tombola,
I forgot to give print $error...i did that..now it works...but therez another problem now it is printing even the lines in the script which has the word "error" along with the actual errors ni the logfile.Is there any way to get rid of this?