Pass perl variable to sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass perl variable to sed
# 1  
Old 07-17-2006
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.
# 2  
Old 07-17-2006
sed -f <sed program file name>

You can write sed commands to file from perl; then execute the command file with -f
# 3  
Old 07-17-2006
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 05-31-2008
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.
# 5  
Old 07-14-2008
If anyone is interested in answering this question, I'd like to know how to do it too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Red Hat

pass a variable line number to sed

num=10 sed -n '$num p' test.txt sed -n '10 p' test.txt works however i am putting the sed command in a loop and the line number is not static Can someone please help me how to achive this. (1 Reply)
Discussion started by: figure20012
1 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

5. Shell Programming and Scripting

PERL script -- calling 'sed' by passing 'variable value'.

Hi Friends, I'm calling 'sed' command inside one perl script, which is to list directory names which are having some date value as their names (in the form YYYYMMDD) with in the range (start and end date). #!/usr/bin/perl -w use strict; use warnings; my $DATA = "/export/home/ganapa"; my... (5 Replies)
Discussion started by: ganapati
5 Replies

6. Shell Programming and Scripting

pass variable to sed like in awk (-v switch)

hi all is possible to pass shell (bash) variable to sed like it is in awk? example: awk script is storred in awk.awk file and I am passing variable called var to this file. $ cat awk.awk {if ($5==var) print $0} so it works when i issue $ bash_var=24 $ ls -l | awk -v... (1 Reply)
Discussion started by: wakatana
1 Replies

7. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: alex_5161
4 Replies

8. Shell Programming and Scripting

pass a variable to sed p in a loop?

Hi, Thanks for looking,,,, (kornshell) tmp3 is a list of line numbers I want to print the lines from my list code: while read j do echo $j #works fine echo $filename #works fine #sed "'$jp'" "$filename" ... (7 Replies)
Discussion started by: JohnMario
7 Replies

9. Shell Programming and Scripting

Pass csh variable to Perl

Hi All, I am trying to put the csh variable into a perl. In the below case, i am trying to put the csh variable "var" into my perl code. I tried to use '"$var"' but i don;t think it works. Can anybody help me pls? #!/bin/csh set var = `echo "xxx"` perl myperlcode.pl file ... (9 Replies)
Discussion started by: Raynon
9 Replies

10. Shell Programming and Scripting

Pass variable to sed?

Hi there. If variables are named inside of a ksh script, how is it possible to pass these to sed? The affected portion of my script goes something like this: A=`cut -d. -f1 $FILE` B=`cut -d. -f2 $FILE` C=`cut -d. -f3 $FILE` sed 's/1111/$A/g;s/2222/$B/g;s/3333/$C/g' file > anotherfile ... (2 Replies)
Discussion started by: kristy
2 Replies
Login or Register to Ask a Question