Use of sed inside perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use of sed inside perl
# 1  
Old 10-16-2015
Use of sed inside perl

Code:
$string="test.binary.value";
$toggle="test.binary.value=true";
host=test.server.com

my $returnCode = system ( "ssh $host 'cp -p /tmp/testfile /tmp/testfile.bkup; sed -i 's/\$string.*/\$toggle/g' /tmp/testfile'" );

cat testfile

test.binary.value=false

Scenario: I am trying to change the value of that binary to true from false. The user provides $toggle as an argument to the perl script. I want to change the line what ever be the previous line in the file to what user entered. Its like search and replace.

The above is my code but its not working, but its simply making the file empty/null. I am not sure whats the mistake I am making. Any help would be greatly appreciated.
# 2  
Old 10-16-2015
Instead of
Code:
my $returnCode = system ( "ssh $host 'cp -p /tmp/testfile /tmp/testfile.bkup; sed -i 's/\$string.*/\$toggle/g' /tmp/testfile'" );

try
Code:
my $returnCode = qx( ssh $host "cp -p /tmp/testfile /tmp/testfile.bkup; sed -i s/$string.*/$toggle/ /tmp/testfile" );

This User Gave Thanks to Aia For This Post:
# 3  
Old 10-16-2015
This is not Perl. This is 99% shell code in a transparently thin wrapping of perl. You could dispense with the Perl and hardly see a difference:

Code:
string="test.binary.value"
toggle="test.binary.value=true"
host=test.server.com

ssh "$host" "cp -p /tmp/testfile /tmp/testfile.bkup ; sed -i 's/$string.*/$toggle/g' /tmp/testfile"
returnCode="$?"

Also, perl wastes an entire shell every time you do system("something"); so if your perl code mostly resembles this, it might be better to do everything in 1 shell than 37.

Last edited by Corona688; 10-16-2015 at 12:39 PM..
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 10-19-2015
This is a snippet from a CGI code. I agree that I can do it in shell. But the situation is not permitting. :P
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed inside shellscript

Hi All, I am trying to use sed command inside a shell script. The same sed command is working on command line, however its not working while using inside a shell script. From various sources i found , it could be done by using -i option, but its not working as well. sed... (11 Replies)
Discussion started by: gotamp
11 Replies

2. Shell Programming and Scripting

Rewrite sed to perl or run sed in perl

I am having trouble re-writing this sed code sed -nr 's/.*del(+)ins(+).*NC_0{4}(+).*g\.(+)_(+).*/\3\t\4\t\5\t\1\t\2/p' C:/Users/cmccabe/Desktop/Python27/out_position.txt > C:/Users/cmccabe/Desktop/Python27/out_parse.txt in perl Basically, what the code does is parse text from two fields... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. UNIX for Dummies Questions & Answers

Array inside sed

Hi guys Let me at first describe the whole thing that I'm trying to do. Lets say I have 100 files like the following one. Ow 1230 16.000000 -0.834000 16.083957 1.751652398 -17.20094528 -4.450623277 Hw 1231 ... (6 Replies)
Discussion started by: saleheen
6 Replies

4. Shell Programming and Scripting

Perl inside shell

I am trying to find out the difference between two dates, for which I am using perl inside SHELL. Below are my codes. perl -MDate -e 'Date::Calc qw(Delta_DHMS);' perl -e '($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)' But... (6 Replies)
Discussion started by: sauravrout
6 Replies

5. Shell Programming and Scripting

Variable inside sed

Problem: Need to read contents from a file and use that value inside sed as avariable. sample code below. THe below code replaces contents inside file123 for matched string with "$x" value only. but we want the value of x which is read from TextFile.txt to go in there. cat TextFile.txt|while... (3 Replies)
Discussion started by: cvsanthosh
3 Replies

6. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

7. Shell Programming and Scripting

Need help using sed inside the loop.

Hi, i have written a script. it collects data based on the sql queries executed by it. i have multiple output files. after the output file is made i need to do some cosmetic changes in the files and then store them. i am unable to use sed conditions inside the loop. see below code for... (3 Replies)
Discussion started by: dazdseg
3 Replies

8. Shell Programming and Scripting

PERL: an OR inside an IF .. im stuck

Hi all, I basically have an if statement that says if you have more than 5 changes you need to have the force flag set to 1 to proceed. like this if ((($Changes > 5) && ($force == 1)) || (($nChanges > 0) && ($Changes < 6))) { I now need to allow the force flag to be either 1 or 2 to... (2 Replies)
Discussion started by: rethink
2 Replies

9. UNIX for Dummies Questions & Answers

SED inside while loop

Hi, im having problem creating a loop using my code: aside from the fact that the 1st variable (VAR) does not increment, it loops more than the expected output. for sample purposes, test csv contains 3 lines. #get number of lines in the file lines=$( wc -l < test.csv ) ... (5 Replies)
Discussion started by: paoie
5 Replies

10. Shell Programming and Scripting

Using Perl Inside KSH

Hi there, I am new to Perl and KSH. The system I am using picks up the KSH scripts and uses them in a batch job control system. I am trying to set a variable from a perl command #!/bin/ksh -eaxp #******************************************************************************* # Testing... (5 Replies)
Discussion started by: SaadLive
5 Replies
Login or Register to Ask a Question