Why does this 'sed' fail inside qx() in Perl? - perl

This sed works, to replace the value for Java home in a shell script:
sed -i 's#^JAVA_HOME=.*$#JAVA_HOME="/usr/lib/jvm/java-1.7.0-oracle.x86_64"#' /apps/tempbsu.sh
but now I am trying to use/invoke that sed from inside a Perl app, using qx():
qx(sed -i 's#^JAVA_HOME=.*$#JAVA_HOME="/usr/lib/jvm/java-1.7.0-oracle.x86_64"#' /apps/tempbsu.sh);
and when I do that, I am getting an error:
sed: -e expression #1, char 58: unterminated `s' command
From checking, I gather that error is happening because the sed is missing the last delimiter, but it seems like it is correct, i.e.:
sed -i 's#.....#.......#' /apps/tempbssu.sh
Can someone tell me why this sed is failing with I use in a qx() in Perl?

$#JAVA_HOME is treated as a Perl variable (the number of the last element of the array variable). Escape it: \$#JAVA_HOME

Related

sed - 'extra character after the command'

having trouble with a sed command.
I'm looking to find a line in a file and replace it.
In my script I've used this command without issue; (I use it to set variables)
sed -i '/job=empty/c\job='$job'' $sd/pingcheck-mon-$job.sh
The line I want to replace looks like this,
bash home/user/pingcheck/pingcheck-jobs/job1/pingcheck-mon-job1.sh
This is the command I can't get to run:
sed -i '/bash '$sd'/pingcheck-mon-'$job'.sh/c\jobslot=empty' $wd/pingcheck-worker.sh
Error I get:
sed: -e expression #1, char 9: extra characters after command
Could someone please tell me where I'm going wrong?
Thanks in advance!

Execute sed command inside TCL script

I am trying to execute sed command inside TCL script . Basically i wanted to remove all empty lines from the input file before reading the file using TCL. so i tried following in my script
exec sed -i '/^\s*$/d' .tmp.PG_Ring
set fid [open ".tmp.PG_Ring" r]
But the script is dumping following Error .
sed: -e expression #1, char 1: unknown command: `''
while executing
"exec sed -i '/^\s*$/d' .tmp.PG_Ring"
(file "pg_ring.tcl" line 1)
could you please provide me work around for this & help me with best way to do this?
That won't work, as single quotes have no special meaning to Tcl at all. Tcl uses braces to mean the same sort of thing (except they nest nicely), so instead you can use this:.
exec sed -i {/^\s*$/d} .tmp.PG_Ring

How to output the contents of a variable to a newline using sed

PING=$(ping -c 2 google.com)
sed -i.bak "s/test:/&\n$PING/g" test.txt
Im trying to output the variable PING on a newline after test: in the test.txt file.
But i keep receiving this error.
sed: -e expression #1, char 64: unterminated `s' command
I don't know where I'm going wrong any help is much appreciated.
you can write sed scripts like you write bash scripts, and newlines are command separators. you need a line continuation.
untested:
sed "s/test/&\
$PING/g" file
Style advice: get out of the habit of using UPPERCASE variable names. One day you'll use PATH and break your script.

sed change line error message

I have a config file I need to change (again) and the line is
set wrapper_code=C:\windows\drivers\cache
I need to change it to
set wrapper_code=/home/harry/solo/run
I wrote
cat Proxy.bat | sed -i.bk -e 's/\(^set wrapper_home\=\).*/\/home/'1${dbuser}'/gateway/service\' Proxy.bat
I get an error message
sed: -e expression #1, char 37: unknown option to `s'
What is wrong with my code string
If you are using / as the pattern separator is sed, you have to escape the slashes in the strings (paths). To avoid it, use a different separator:
sed -i.bk -e 's%\^set wrapper_code=C:\\windows\\drivers\\cache%set wrapper_code=/home/harry/solo/run%' Proxy.bat
You also have to escape backslashes, as they have a special meaning in sed.
The cat part is useless.

About replacing string with sed

I'd like to replace all the \r\n with < br/ >in a document, and I'm trying this see script below
# sed -i 's/\r\n/<br/>' ~/xxd/*
however i got this error back
sed: -e expression #1, char 12: unknown option to `s'
How do i solve this problem?
Thanks!
Your problem is that you have the / separator in your replacement string so sed is assuming that's the end of your replacement, and that the > following it is a flag.
If your sed is modern enough, just use a different separator character, one that's not in the replacement string:
pax$ echo hello | sed -e 's/e/<br />/'
sed: -e expression #1, char 9: unknown option to `s'
pax$ echo hello | sed -e 's?e?<br />?'
h<br />llo
Alternatively, you can escape the offending character but I try to avoid that since it tends to lead to overly sawtooth sed commands like /\/\/\/\/\/\.
The other thing you may want to watch out for is trying to use \n in your regex since sed operates on lines anyway. If your intent is to just strip carriage returns and insert HTML line breaks, then the following sed command may be better:
s?\r$?<br />?