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

I am using: `grep "]Send Started" /user/telalert/telalert.trail`; in a perl script, however I need to assign it to a variable. I have tried several combinations including: $TTRAILS="`grep ']Send Started' /user/telalert/telalert.trail`"; and open(Trails,"`grep ']Send Started' /user/telalert/telalert.trail`|")||die "Cannot open.\n"; but it won't work....can someone help me please!!!! Thank you OH Wise One's in advance!!! BJB

Edit: chipmunk 2001-05-10

Replies are listed 'Best First'.
Re: asigning a variable to a grep statment
by suaveant (Parson) on May 10, 2001 at 23:04 UTC
    You almost had it... simply
    $TTRAILS=`grep "]Send Started" /user/telalert/telalert.trail`;
    will do it.
                    - Ant
Re: asigning a variable to a grep statment
by ZZamboni (Curate) on May 11, 2001 at 02:12 UTC
    Apart from suaveant's response, note that this is not the most efficient or portable way of doing the search. Which may be OK if this is quick script, but may not if it's intended to be a production program, or to run in multiple platforms.

    One Perl-only way of doing it could be like this:

    open(F, "/user/telalert/telalert.trail") or die "Error: $!\n"; @TTRAILS=grep /\]Send Started/, <F>; close(F);
    You could join @TTRAILS together if you want a single string, which is what the grep command would return.

    I'm not sure if the grep on <F> will read the whole thing in memory at once or if it is intelligent enough to read it line by line, which could make a difference if the file is very large.

    --ZZamboni