s_gaurav1091 has asked for the wisdom of the Perl Monks concerning the following question:

hi monks, I am struck up at a place.I am storing a value in one variable and then I am trying to find that variable from a file with the help of grep. for example my $find = "ObjectEventReceiverService"; now I am executing a command through the script
my $date = `date +20%y/%m/%d `; @array = `grep $date $ENV{JBOSS_HOME}/log/wrapper.log |grep "$find sta +rted"`;
My problem is that the grep command is not taking the value of $find as "ObjectEventReceiverService" instead its searching for $find.Can anyone suggest that what I should do here????

Replies are listed 'Best First'.
Re: literal meaning of a variable
by brian_d_foy (Abbot) on Feb 27, 2006 at 05:54 UTC

    Is this your actual code? You'll probably want to chomp that date because it's coming with a newline with messes up the grep. Even better would be doing that directly in Perl. :)

    The backticks are a double-quoted context, so variables interpolate. Ensure you're getting what you think you're getting by changing the backticks to a print.

    print qq(grep $date $ENV{JBOSS_HOME}/log/wrapper.log | grep "$find sta +rted");
    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: literal meaning of a variable
by McDarren (Abbot) on Feb 27, 2006 at 06:01 UTC
    Can anyone suggest that what I should do here????

    Well, yes.
    If it was me - rather than calling the shell - I would open the file, and then search for the line/s of interest.
    Something like (untested):

    my $file = "$ENV{JBOSS_HOME}/log/wrapper.log"; open IN, "<$file" or die "Cannot open $file:$!\n"; while (<IN>) { chomp; next if ($_ !~ /$find/); next if ($_ !~ /$date/); push @array, $_; }

    Cheers,
    Darren :)

    Update: Fixed code snippet, as per revdiablo below :)

      Except !~ is a binary operator. Perhaps you meant:

      ... next if !/$find/; next if !/$date/; ...

      Though I would be tempted to instead use:

      ... next unless /$find/; next unless /$date/; ...
        Except !~ is a binary operator.

        heh... well, I did say it was untested :)
        What I meant was: next if ($_ !~ /$find/);
        Although I guess in that context it's essentially the same as: next if !/$find/; yes?

        Ever since I read TheDamian's PBP, I tend to try and avoid unless :)