How to output the contents of a variable to a newline using sed - 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.

Related

Why does this 'sed' fail inside qx() in 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

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

sed append text after match

I am writing a shell script to be able to append text after the match is found in a file
for example, in ~/.bash_profile file for the following line
PATH=$PATH:$HOME/bin
we need to append it with :/usr/java/jdk1.6.0_38/bin
so it'll become the following
PATH=$PATH:$HOME/bin:/usr/java/jdk1.6.0_38/bin
how could I do it with sed?
I tried with the following command from inside the console first, but it gave me error complaining 'sed: -e expression #1, char 13: unknown option to `s''
sed '/PATH/s/$/:/usr/java/jdk1.6.0_38/bin' ~/.bash_profile
what's wrong with my command above?
The problem is that you have regex delimiters in the replacement part of the substitute command. Either escape them with \ or use a different delimiter (comma in this case):
sed '/PATH/ s,$,:/usr/java/jdk1.6.0_38/bin,' ~/.bash_profile
This might work for you (GNU sed):
sed 's|PATH=$PATH:$HOME/bin|&:/usr/java/jdk1.6.0_38/bin|' ~/.bash_profile

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 />?