Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

bash loop passing to perl command line

by ZWcarp (Beadle)
on Oct 26, 2011 at 19:05 UTC ( #933971=perlquestion: print w/ replies, xml ) Need Help??
ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I'm trying to figure out how to pass a bash variable to a perl command line executable.

for i in 1 2 3 4 5 6 7 10 11 12; do perl -lane 'print p${i}. "\t$_"' p${i}.chr7.miRNA.txt; done

The idea is just to have each file be edited to include the "p" number as the first column. The files are set up as p*.chr7.miRNA.txt where the * corresponds to the patient number. Any help would be great! Thanks!

Comment on bash loop passing to perl command line
Download Code
Re: bash loop passing to perl command line
by choroba (Canon) on Oct 26, 2011 at 20:19 UTC
    Do not enclose the variable to be expanded into single quotes:
    for i in 1 2 3 4 5 6 7 10 11 12 ; do perl -lane 'print p'$i'. "\t$_"' p$i.chr7.miRNA.txt done

      Drop out of single quotes:

      for i in 1 2 3 4 5 6 7 10 11 12 ; do perl -lane'print "p'$i'\t$_"' p$i.chr7.miRNA.txt done

      Or use double quotes: (Don't forget to escape what needs to be escaped.)

      for i in 1 2 3 4 5 6 7 10 11 12 ; do perl -lane"print qq{p$i\\t\$_}" p$i.chr7.miRNA.txt done

      Or pass it as an argument:

      for i in 1 2 3 4 5 6 7 10 11 12 ; do perl -lane'BEGIN { $i = shift(@ARGV); } print "p$i\t$_"' $i p$i.chr7.miRNA.txt done

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2015-07-05 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls