D. Crouse wrote:
I have a perl -e function in my .bashrc file.
This sources in the perl -e function so I can run it by just the command name.
I'm having trouble with the substitution of my $1 bash variable into
the perl -e function.

Here is what I have so far.

grepi ()
{
perl -ne 'BEGIN {$/ = "\n\n"} print if /$1/' < $2
}


The $2 is fine.....it works as expected, if I substitute a WORD for
$1.  It is the $1 that is giving me all sorts of grief.
I've searched google, and am finally giving up figuring it out all by
myself.  ;)

Basically what it does is grep out an entire paragraph at a time,
emulates a tru64 "grep -i" search.
Works fine if I enter $1 in as a word, so I know I'm close :(

First, Perl provides a switch for paragraph mode so you don't need the BEGIN block.

Your problem happens because in the shell, as in perl, single quotes do not interpolate so the variable $1 is not seen by the shell at all but it is seen by perl. You need to either enclose the perlcode in double quotes:

grepi ()
{
perl -00ne "print if /$1/i" < $2
}


Or pass the contents of $1 to perl:

grepi ()
{
perl -s00ne 'print if /$r/i' -- -r="$1" < $2
}


See:

perldoc perlrun

for details on the -0 and -s switches.



John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to