Perl script question

G

George Monappallil

Hi Guys:
Below is my code.
How can I grab the last data of the first column of the output of egrep -n
and store it in a file for comparing it against the last data next time the
script is run.
Is there an easier way of doing this?
Your help is appreciated.

------------------------------
#!/bin/perl

$LOG = "./messages.log";

$SIG{"INT"} = "clean_exit";
$SIG{"TERM"} = "clean_exit";

open(FILE, $LOG) || die "Can not open file $LOG";

# Main loop.
$WORD = "ERROR";
if ($LOG) {
$CURRLINECNT=`egrep -n $WORD $LOG | awk -F":" '{print $1}'`;
# THIS DOES NOT WORK EITHER. What's wrong in the syntax ?
}

# We jump here in case of INT or TERM signals
sub clean_exit {
($signal) = @_;
close FILE;
print "$file closed.\nExiting on signal $signal...\n";
exit;
}
--------------------------
 
J

Jürgen Exner

George said:
Hi Guys:
Below is my code.
How can I grab the last data of the first column of the output of
egrep -n and store it in a file for comparing it against the last
data next time the script is run.
Is there an easier way of doing this?
Your help is appreciated.

You are missing

use warnings;
use strict;
$LOG = "./messages.log";

$SIG{"INT"} = "clean_exit";
$SIG{"TERM"} = "clean_exit";

Useless use of double quotes. In this particular case they don't cause any
harm, but that's not always the case.
There is a FAQ about the topic.
open(FILE, $LOG) || die "Can not open file $LOG";

You may want to add the reason _why_ the open failed, see variable $!.
# Main loop.

But there is no loop anywhere here
$WORD = "ERROR";
if ($LOG) {
$CURRLINECNT=`egrep -n $WORD $LOG | awk -F":" '{print
$1}'`; # THIS DOES NOT WORK EITHER. What's wrong in the syntax ?

Gaaaaaack! Why do you fork an external process instead of using Perl's
buildin functions?
Also, if you would have used warnings and strictures, then perl would have
told you quite a few things about this line like e.g that $1 is undefined
because you never had a successful pattern match in your program.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top