Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comandline arguements when perl code is getting executed from command line

by jesuashok (Curate)
on Jan 16, 2007 at 03:00 UTC ( [id://594841]=perlquestion: print w/replies, xml ) Need Help??

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

fellow monks,

#!/usr/bin/ksh cat file | while read i do file_name=`echo $i | awk -F"|" '{print $1}'` #get the file_name export type=`echo $i | awk -F"|" '{print $2}'` #get the customer type + setting for the file perl -nle '/TABLE/ && print "$ARGV:$_:$ENV{type}"' $file_name done
In the above shell script, I am getting the filename and type of file, this both has to be printed when a particular pattern matches in the filename.
Through $ARGV I can get the filename and get printed. How can I print the `type` without using `export type` and $ENV{type} in print statement ?

  • Comment on Comandline arguements when perl code is getting executed from command line
  • Download Code

Replies are listed 'Best First'.
Re: Comandline arguements when perl code is getting executed from command line
by merlyn (Sage) on Jan 16, 2007 at 03:06 UTC
    First, you have a useless use of cat.

    Second, why are you doing shell at all? Why not do this all in Perl?

    open my $f, "<", "file" or die; while (<$f>) { chomp; my ($file_name, $type) = split /\|/; open my $inner, "<", $file_name or die; while (<$inner>) { if (/TABLE/) { print "$file_name:$type\n"; # last ? } } }
      hi merlyn,

      thanks for giving me( all monks too ) a nice note on "useless use of cat". My shell script does lot of other task also. I have just copied a required part and asked in perlmonks. Just I am curious to know how that can be done ? that is all. Doing everything in perl is not a bad idea at all.

      thanks a lot merlyn.

Re: Comandline arguements when perl code is getting executed from command line
by ikegami (Patriarch) on Jan 16, 2007 at 03:57 UTC
    Pass it as a parameter, and it'll be in @ARGV.
      Not a good idea when using the -n switch. Unless you mean a BEGIN { } block and maybe Getopt::*?
        He could use BEGIN { $type = shift(@ARGV); }, he could use -s or he could get rid of -n.
      A reply falls below the community's threshold of quality. You may see it by logging in.
      #!/usr/bin/ksh cat file | while read i do file_name=`echo $i | awk -F"|" '{print $1}'` #get the file_name export type=`echo $i | awk -F"|" '{print $2}'` #get the customer type + setting for the file perl -nle '/TABLE/ && print "$ARGV:$_:@ARGV"' $file_name $type done
      thanks a lot, the above too worked fine as expected.

        hi, don't you have a shell that supports $(...) to avoid backquotes `...`; the first does nest properly and the second does not. Modern shell hackers consider the second form obsolete, but yes it is portable and is probably the way to go if you do configure-like sport...

        cheers --stephan

        Not really. -n tries to open the value of $type as a file.

        By the way, won't you have a problem if $file_name or $type contains shell metacharacters?

Re: Comandline arguements when perl code is getting executed from command line
by Thelonius (Priest) on Jan 16, 2007 at 06:39 UTC
    This should work:
    perl -slne '/TABLE/ && print "$ARGV:$_:$type"' -- -type=$type $file_na +me
    See the -s swith in the perlrun manpage.
Re: Comandline arguements when perl code is getting executed from command line
by jdporter (Paladin) on Jan 16, 2007 at 16:00 UTC

    ksh is a pretty decent scripting language, actually.

    #!/usr/bin/ksh while read i do file_name=${i%\|*} cust_type=${i##*\|} if ! [[ -z "$file_name" || -z "$cust_type" ]] then while read j do if [[ "$j" = *TABLE* ]] then print "$file_name:$j:$cust_type" fi done < $file_name fi done < file
    A word spoken in Mind will reach its own level, in the objective world, by its own weight

      I do agree ;) the latest version ksh93s has just been released and offers quite a lot of new goodies (regex extensions and lots of utf8 fixes). The extension mechanism that has been there since the beginnings of ksh93 in 93 ;) is quite. By the way as cat is a builtin of modern ksh (and the last command of the pipeline in ksh is executed by main()) the efficiency of 'cat...| cmd' is (usually) hardly a problem. From the command line, and I often use a cat like this, in case I need to edit later to put 'cat -evnt' o even to sandwich some extra "line" like this

       { echo; cat ...; echo; } | whatever

      But... there is an exception actually 'cmd | while read' is probably one of "worst" constructs in terms of efficiency as the shell needs to read one byte at a time in case another command in the body of the while could compete for stdin. If it is *not* the case, a trick like this (for modern enough shells) is nice:

      use 'cmd | while read -u9 ...; done 9<&0-'
      desc 0 is duped on desc 9 and then closed

      # an example of healthy competition for stdin while read header do dd ... done

      Note that you need a true move-close and not 9>&0 0<&- as by the time the dup is done, the close doesn't know about it and does not set a share flag. ksh88 does not have the logic needed for example. If your system pipes are dumb (no peek ahead) the trick does not work either.

      I finish with some hack. the 'while < file' idiom does that move-close magic under the hood (and does a 'set' on each line read) and is quite efficient. "under cover select" is what you get if you stack them 'while <file1 <file2 ...' ;)

      % stephan@labaule (/home/stephan) % % $0 --version; cat -n hi1 version sh (AT&T Labs Research) 1993-12-28 r 1 just 2 another 3 ksh 4 hacker % stephan@labaule (/home/stephan) % % while < hi1; do print -n $1' '; done; echo just another ksh hacker

      David Korn's own words on this (thread of ast-users mailing list on july 2006) are interesting from a un*x perspective: https://mailman.research.att.com/pipermail/ast-users/2006q3/001084.html

      enjoy --stephan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2025-06-19 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.