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

The following works, but I would like to feed in the value for "Vet" as an "ARGV[0]" Could anyone help please? perl -n -a -e 'if (($_ = "$F1\n") =~ "Vet") { print }' TEST.txt

Replies are listed 'Best First'.
Re: Feed ARGV into one-liner
by almut (Canon) on Jan 21, 2010 at 14:51 UTC

    You can use a BEGIN block to operate on @ARGV (the $v=shift below, shifting off the first arg) before the implicit while (<>) {} loop (behind -n) takes effect treating the remaining arguments as file names.

    $ perl -n -a -e 'BEGIN {$v=shift} if (($_ = "$F[1]\n") =~ /$v/) { prin +t }' Vet TEST.txt
      Thanx, that is great!
Re: Feed ARGV into one-liner (the sane way :)
by BrowserUk (Patriarch) on Jan 21, 2010 at 15:17 UTC

    Another way. use perl's builtin argument parsing:

    c:\test>perl -sane"print if $F[ 1 ] =~ /$T/" -- -T=vet test.txt b vet c f vet g

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Feed ARGV into one-liner
by JavaFan (Canon) on Jan 21, 2010 at 17:02 UTC
    Don't underestimate the power of command line switches:
    perl -MB';$'F=shift -ane '$F[1]=~$F&&'print -- Vet TEST.txt
      Can you explain this please? In particular what does B';$'F=shift do? It's melting my head.... thanks
        Shell literal
        -MB';$'F=shift
        produces the arg
        -MB;$F=shift
        You're probably familiar with -M. It used to load a module. It does so by inserting
        use MODULENAME;
        into the source. In this case, MODULENAME is B;$F=shift, so
        use B;$F=shift;
        is inserted into the source. B is a core module. It's not being used at all by the program. The only purpose to load it is to execute $F=shift; before the -n loop with very few keystrokes. The following would also work; it's just not nearly as short:
        '-MData::Dumper; $F=shift'
Re: Feed ARGV into one-liner
by Anonymous Monk on Jan 21, 2010 at 14:42 UTC
    What do you call a question which answers itself?
    perl -lane ' print $F[1] if $F[1] =~ $ARGV[0] '
      But how do I give it the filename and the search value?
        Whooops
Re: Feed ARGV into one-liner
by Krambambuli (Curate) on Jan 22, 2010 at 16:52 UTC
    A bit OT to your direct question, but just as an idea:

    it's sometimes useful and easy to also use the shell as a helper.

    You could write a shell script vet.sh containing

    perl -ne 'print $_ if /'$1'/' $2

    which invoked as

    ./vet.sh Vet Test.txt

    would also do what you asked for. As a side benefit, your oneliner is written down - just in the case you do not remember it well a week later when you need it again. :)


    Krambambuli
    ---
      cat >>oneliners.txt #frobnicates the fetzer valve perl -ne 'print $_ if /'$1'/' $2 ^D