|
|||||||||
| Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here. |
unix and linux commands - unix shell scripting |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Pass perl variable to sed
Hello,
I'd like to pass a variable to a sed command in a perl script. The script is like this : #!/usr/bin/perl -w $newline="new"; system q(sed '/insert/ i\ '$newline <sed1.txt >sed2.txt); But the interpretor wouldn't recognize $newline, it inserts a "\n" instead. I've also tried : system q(sed "/insert/ i\ $newline" <sed1.txt >sed2.txt); Then I got the same result. Anyone has an idea? Thanks. |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
sed -f <sed program file name>
You can write sed commands to file from perl; then execute the command file with -f |
| Sponsored Links | ||
|
|
|
#3
|
||||
|
||||
|
Thank you, Jim.
But then I need to call "sed -f .." in the perl script, and pass the perl variables to the sed script. It seems to complicate the problem. I've found out that might be a concatenation problem in my system function. I rewrited it like this : system('sed -e "/insert/ i\\'.$newline.'" <sed1.txt >sed2.txt'); And it works. Now I have a new problem. It wouldn't let me use "+" for the regular expression. For example, system('sed -e "/insert+/ i\\'.$newline.'" <sed1.txt >sed2.txt'); It fails to match "inserttt". But with "*", it works well. |
|
#4
|
||||
|
||||
|
I have a similar issue. I also need to pass a perl variable into the sed command as follows: Code:
system("sed -e s/",$test[0],"/",$test[1],"/g file > output");Basically, I want to have my perl script call the sed command to search for the string in subscript 0 of the 'test' array and replace it with subscript 1 of the 'test' array. I tried hi_ryo's method, but it didn't work in my case. |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
If anyone is interested in answering this question, I'd like to know how to do it too.
|
| Sponsored Links | ||
|
|
![]() |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to pass value of pwd as variable in SED to replace variable in a script file | cielle | Red Hat | 6 | 03-28-2012 03:47 AM |
| How to pass a function with a variable parameter into another variable? | U_C_Dispatj | Shell Programming and Scripting | 2 | 05-26-2011 12:44 PM |
| Perl - pass shell-vars into perl for input loop | alex_5161 | Shell Programming and Scripting | 4 | 03-19-2010 03:11 PM |
| Pass csh variable to Perl | Raynon | Shell Programming and Scripting | 9 | 10-19-2007 10:46 PM |
| How to pass a variable to Awk ? | Raynon | Shell Programming and Scripting | 24 | 02-26-2007 09:25 AM |
|
|