Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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

Replies are listed 'Best First'.
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
      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 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.

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.


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 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://368544]
Approved by Old_Gray_Bear
[Discipulus]: ah! so you mean if i need a static image i can use Twig, if on the fly better css selector and mody attrs (jquery?), rigth?
[LanX]: not sure what you guys are taking about, but a DOM manipulation in JS shouldn't be difficult
[Corion]: Discipulus: Yeah, that's what I would try (but so far, I can't even change the colors manually in the SVG, which is why I gave up ;) )
Discipulus bad sign..
[Discipulus]: italian doc says to modify somehow the initial SVG before using it with D3.js
[Discipulus]: LanX the fact is that my js ability is even poorer than my Perl one..
[LanX]: oh its negative? ;P
[Discipulus]: my warmest comprehension karlgoethebier ;=(

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2017-03-23 09:48 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (285 votes). Check out past polls.