Perl script question

Discussion in 'Perl' started by George Monappallil, Feb 15, 2005.

  1. 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;
    }
    --------------------------
    George Monappallil, Feb 15, 2005
    #1
    1. Advertising

  2. George Monappallil wrote:
    > 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


    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
    Jürgen Exner, Feb 15, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,516
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,791
  3. pj
    Replies:
    3
    Views:
    283
  4. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    137
    Paul Lalli
    Jan 5, 2005
  5. Replies:
    20
    Views:
    704
    Gunnar Hjalmarsson
    Jan 18, 2005
Loading...

Share This Page