Unix/Linux Go Back    


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

Pass perl variable to sed

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-17-2006   -   Original Discussion by hi_ryo
hi_ryo's Unix or Linux Image
hi_ryo hi_ryo is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 10 March 2008, 1:24 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old Unix and Linux 07-17-2006   -   Original Discussion by hi_ryo
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 12 January 2018, 9:55 AM EST
Location: NM
Posts: 11,280
Thanks: 581
Thanked 1,125 Times in 1,036 Posts
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  
Old Unix and Linux 07-17-2006   -   Original Discussion by hi_ryo
hi_ryo's Unix or Linux Image
hi_ryo hi_ryo is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 10 March 2008, 1:24 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old Unix and Linux 05-31-2008   -   Original Discussion by hi_ryo
userix's Unix or Linux Image
userix userix is offline
Registered User
 
Join Date: May 2008
Last Activity: 19 May 2011, 6:23 PM EDT
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old Unix and Linux 07-14-2008   -   Original Discussion by hi_ryo
gelitini's Unix or Linux Image
gelitini gelitini is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 15 August 2008, 10:15 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
If anyone is interested in answering this question, I'd like to know how to do it too.
Sponsored Links
Closed


Linux 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



All times are GMT -4. The time now is 12:33 PM.