Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

More Arguments

by joaming (Novice)
on Sep 05, 2015 at 03:31 UTC ( [id://1141111]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am new to Perl, learning how to parse a linux log file. So, I grep sth from the log, serving as input to m parse script and print the result to a proper format.

[linux host] $ grep "Something" sample.log | perl myParser.pl my $line; foreach $line (<>) { ... }

This works fine, but I would like to supply more parameter to my perl script to determine the format style, eg.

$ grep "Something" sample.log | perl myParser.pl VERTICAL
will print the result vertically,
$ grep "Something" sample.log | perl myParser.pl COLUMN
will print the result in columnized format.

I tried to capture it by $ARGV[0], $ARGV1, but still failed. Please kindly help

More, i did a simple test

#!/usr/bin/perl use strict; my $param0 = $ARGV[0]; my $line; print "\nFirst Arguement is : $param0"; foreach $line (<>) { ... }

$ grep "Name=" sample.log | perl SimpleTest.pl VERTICAL

Can't open VERTICAL: No such file or directory at SimpleTest.pl line 1 +1. First Arguement is : VERTICAL

Then , it just stopped and not execute the foreach loop

Replies are listed 'Best First'.
Re: More Arguments
by Athanasius (Archbishop) on Sep 05, 2015 at 04:31 UTC

    Hello joaming,

    To get the syntax right, you’ll need to study perlop#I/O Operators:

    The null filehandle <> is special: it can be used to emulate the behavior of sed and awk, .... Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input....

    By trial and error, I found the following (on Windows), which seems to do what you’re looking for:

    14:17 >echo Hello| perl -wE "$x = <>; chomp $x; $y = $ARGV[0]; say qq[ +>$x $y<];" - world >Hello world< 14:17 >

    The call to <> finds - as the first argument on the command line and so reads from standard input, which is Hello\n. After this, @ARGV is apparently reset. I haven’t found that behaviour documented (yet).

    Anyway, hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thank you. I still have a long road to walk on Perl, lol
Re: More Arguments
by choroba (Cardinal) on Sep 05, 2015 at 07:18 UTC
    Don't use the bare diamond <> if you want to handle arguments yourself and read from the standard input. Use <STDIN> instead:
    echo input | perl -we 'print "ARGS: @ARGV\n"; print while <STDIN>' fil +e.txt arg2
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: More Arguments
by stevieb (Canon) on Sep 05, 2015 at 03:37 UTC

    Hey, joaming, welcome to the Monastery.

    Could you please provide some of your input data, expected output and a bit more of your code? This will help us determine where we can improve on what you've already got.

    -stevieb

      I am sorry I didn't address what I encountered at the first place accurately. Now I updated with a SimpleTest.pl, I expect the code continue to run and enter the foreach loop, but it didn't.
Re: More Arguments
by parv (Parson) on Sep 05, 2015 at 03:35 UTC

    Show your code where you try to use @ARGV & fail.

Re: More Arguments
by QuillMeantTen (Friar) on Sep 06, 2015 at 14:12 UTC

    You can keep the code you wrote and just use xargs that way you will call your script multiple times with just one argument
    Update: oops my bad I misread what you wrote, please disregard

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found