Perl - pass shell-vars into perl for input loop

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - pass shell-vars into perl for input loop
# 1  
Old 03-19-2010
Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable

Something like:
perl -p -e 's/$shell_srch/$shell_replace/g' input.txt

I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.)
I have searched and found, but it doesn't work

Is it possible (without havy sysntaxis, as ENV(..), for exmpl.) in the input loop?
# 2  
Old 03-19-2010
Use doble quotes.

perl -p -e "s/$shell_srch/$shell_replace/g" input.txt

# 3  
Old 03-19-2010
Originally Posted by dennis.jacob
Use doble quotes.

perl -p -e "s/$shell_srch/$shell_replace/g" input.txt

Sorry, it seems I did not explain enough.
My question is how to pass those 2 variables $shell_srch and $shell_replace into processing command.

So, I would have:
replase_txt="system engineer"
  # something like that, but it doesn't work:
perl -p -e "s/$shell_srch/$shell_replace/g" input.txt shell_srch=$search_txt shell_replace=$replase_txt

In such way the perl trying to open files 'shell_srch=..' and 'shell_replace='

I thought it could be done with '-s' option to provide switches from command line, but I can't get it work.

Mayby you know proper sysntax to use '-s' together with -p or -n?
Maybe there is another way to pass shell-vars to the perl command?

P.S. Just realized dennis.jacob your advise: so, this way, with double-quotes, the shell will replace the variables by its values before start the perl.
But, how to make perl be able to work with own variables?
For example, I could need to use the '$_' and '$.' together.

Last edited by alex_5161; 03-19-2010 at 04:19 PM..
# 4  
Old 03-19-2010
Is this what you are looking for ?

$ cat f0
Andy is a programmer.
Bruce is a programmer.
Claire is a programmer.
$ SEARCH="programmer"
$ REPLACE="system engineer"
$ perl -ple "s/$SEARCH/$REPLACE/g" f0
Andy is a system engineer.
Bruce is a system engineer.
Claire is a system engineer.

# 5  
Old 03-19-2010
Originally Posted by durden_tyler
Is this what you are looking for ?
Yes, it is exactly what my example tying to do.
But I am looking for more general way to pass vars.
This way I should escape all special characters that shell would interpret to be passed to perl.
For example:
perl -ne "chomp; print \"\\nLine is:\$_\; \\\$v1=$v1\; \\\$v2=$v2\"; \
  print \"\\n summ=\".($v1+$v2); "  infl.txt

(I think the '-l' is not necessary or it is?)
This way it works, but it is uglee and hard to prepare and debug!

---------- Post updated at 01:11 PM ---------- Previous update was at 12:49 PM ----------

Just found some simplifying with the double-qoutes: it is possible to comby the perl command by multiple quoted strings:
> v1=25;
> v2=30;
> perl -ne 'BEGIN{$v1='"$v1"'; $v2='"$v2"';} chomp; print "$_ | summ=".($v1+$v2)."\n"' input.txt
line one | summ=55
line two | summ=55
third line  | summ=55
one more line | summ=55

This is usefull already, but it is still way around!
It should be a way to do it simpler!

Anybody know?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Shell or Perl Loop Screenshot URLs

I am trying to capture screenshots from a huge list of URLs. I am able to manually capture images of individual pages; that is, I simply run the following command to get a screenshot of $ python /path/to/ I want to modify the script so that instead of... (2 Replies)
Discussion started by: chipperuga
2 Replies

3. Shell Programming and Scripting

Shell/Perl logic for loop

Hi, I have a requirement as follows. Have 3 files. Need to match up the data in each one of them and sum up the data by a field and display it. example given below. File 1 : Name, Emp id File 2 : Empid, Subject, File 3 : Subject, Score, Class Match Emp id in File 1 and File 2 and then... (7 Replies)
Discussion started by: preethgideon
7 Replies

4. Shell Programming and Scripting

Pass string arg from shell to perl

My shell script generates a bunch of lines of text and passes this text as an argument to a perl script. I'm able to do this, but for some reason newlines don't get recognized in the perl script and so the script just prints actual '\n' instead of carriage returning, otherwise everything gets... (3 Replies)
Discussion started by: stevensw
3 Replies

5. Shell Programming and Scripting

need shell or Perl script to read multiple input

I need shell 0r Perl script to read multiple input and do something and come out example: echo “ enter the host names separated by space “ read servers foreach @servers { do do something done} Here host names like host1 host2 host3 . . . . . . . so on Please help me... (8 Replies)
Discussion started by: sreedhargouda
8 Replies

6. Shell Programming and Scripting

Set/Export Env Vars from with Shell Script With Input Variable

I have a shell script I want to run that will set environment variables based on the value of an input variable submitted when the shell script is called. For example: $ prod This would set environment variables for prod $ test This would set environment variables... (1 Reply)
Discussion started by: brtaylor73
1 Replies

7. Shell Programming and Scripting

perl how to exit a while loop and quit reading the input file

I am reading a file using While loop while <FILE> { $_ = <FILE>; process data... } I would like to quit reading the file once I encounter a String pattern. How do i do it. is it if (/SUMMARY/) { last; } I am having problems with uninitialized value in pattern... (1 Reply)
Discussion started by: subhap
1 Replies

8. Shell Programming and Scripting

pass perl variables to shell script

I have a perl script that opens a text file containing numbers on each line: for example: 755993 755994 755995 755996 755997 755998 The perl script takes these numbers and store them as an array @raw_data, where I can access individual numbers by using $raw_data for the value 755993.... (2 Replies)
Discussion started by: xchen89x
2 Replies

9. Shell Programming and Scripting

Can we pass an array of strings from a Perl Program to a Shell Script?

Hi Folks, The subject is my question: Can we pass an array of strings from a Perl Program to a Shell Script? Please provide some sample code. Thanks ---------- Post updated at 11:52 PM ---------- Previous update was at 11:43 PM ---------- I got it. Its here:... (0 Replies)
Discussion started by: som.nitk
0 Replies

10. Shell Programming and Scripting

Difference between use vars and our variable in PERL

What is the difference between defining the global variable through our and using use vars ? Is the variable created using our goes beyond even package scope? Thanks in Advance !!! (3 Replies)
Discussion started by: jatanig
3 Replies
Login or Register to Ask a Question

Featured Tech Videos