Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

perl command line arguments

by jluther (Novice)
on Jun 21, 2004 at 20:24 UTC ( #368544=perlquestion: print w/ replies, xml ) Need Help??
jluther has asked for the wisdom of the Perl Monks concerning the following question:

When typing a one-liner on the command line, is there a way to use arguments while using the -ne switches. e.g. I want to submit an argument to something like this...
perl -ne 'split(/,/);if ($_[7] eq $arg){$cnt++;}END{print "There a +re $arg things in this file\n";}' file_name.txt
Is there a switch that allows me to pass the argument "$arg" in to this command. This way I don't have to type this command all of the time. Thanks. jluther

Comment on perl command line arguments
Download Code
Re: perl command line arguments
by borisz (Canon) on Jun 21, 2004 at 20:32 UTC
    Not really a switch, but what about BEGIN? This should work, the first param is the argument.
    perl-ne 'BEGIN {$arg = shift} split(/,/);if ($_[7] eq $arg){$cnt++;}EN +D{print "There are $cnt things in this file\n";}' arg file_name.txt
    Boris
      Thanks!! I've been on vacation so I am just trying these out. This seems to be the most intuitive. jluther
Re: perl command line arguments
by vladb (Vicar) on Jun 21, 2004 at 20:37 UTC
    You could try setting an environment variable and then accessing it in your perl command via the %ENV hash (works in tcsh shell):
    setenv arg="ARG"; perl -ne '<your command here>'
    Alternatively, you could set the $arg variable at the start of your perl command once:
    perl -ne '$arg="ARG";<rest of your command here>'


    _____________________
    "We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
    the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true."

    Robert Wilensky, University of California

Re: perl command line arguments
by Fletch (Chancellor) on Jun 21, 2004 at 20:39 UTC

    @ARGV works just fine in code from -e.

    Update: D'oh, /me glossed completely over the -n part. The first shift in the BEGIN block's the way I'd do it. Must be Monday . . .

      @ARGV works just fine in code from -e.
      Yep, it does. However, the meaning is changed under -n. Or at least one has to jump through a hoop (namely a BEGIN block) to get to it before the loop created by -n gets to it. But I'm sure you knew that. See perldoc perlrun for more info.

      thor

Re: perl command line arguments
by Roy Johnson (Monsignor) on Jun 22, 2004 at 00:07 UTC
    perl -F, -ane '$arg||=pop; ++$cnt if $F[7] eq $arg }{ print "There are + $cnt $arg s\n"' filename.txt string
    if you're not searching for a single zero.

    We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: perl command line arguments
by itub (Priest) on Jun 22, 2004 at 00:25 UTC

    You can try -s:

    perl -sne 'split(/,/);if ($_[7] eq $arg){$cnt++;}END{print "There +are $arg things in this file\n";}' -- -arg=something file_name.txt

    Note that you need -- to stop perl itself from trying to use -arg as an argument.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://368544]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-12-21 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls