FAQ
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 :(

Thanks

Search Discussions

  • D. Crouse at Mar 30, 2009 at 4:58 pm
    SOLUTION !
    ahhhhhhhhh I got it !!! :)

    You have to use the $ENV

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

    Just an hour or two of working on it.......and it finally pas out ! ! :) WOOT !


    On Mon, Mar 30, 2009 at 11:30 AM, 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 :(

    Thanks
  • John W. Krahn at Mar 30, 2009 at 5:02 pm

    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
  • D. Crouse at Mar 30, 2009 at 5:47 pm
    Thanks :)


    On Mon, Mar 30, 2009 at 12:02 PM, John W. Krahn wrote:
    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-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 30, '09 at 4:30p
activeMar 30, '09 at 5:47p
posts4
users2
websiteperl.org

2 users in discussion

D. Crouse: 3 posts John W. Krahn: 1 post

People

Translate

site design / logo © 2021 Grokbase