writing a special character in sed bash - linux

I have a problem, because I want to replace
QueryConnection::DefaultChannel=
with
QueryConnection::DefaultChannel=/${CHANNEL NUMBER}
e.g:
QueryConnection::DefaultChannel=/5
My code:
sed -i "s/QueryConnection::DefaultChannel=.*/QueryConnection::DefaultChannel=${NUMER_KANALU}/" "${DIR_BOTS}/bot_${COUNT_BOTS}/configTS3AudioBot.cfg"
How to make before ${NUMER_KANALU} be "/"?

Two choices:
Escape the slash:
sed -i "s/QueryConnection::DefaultChannel=.*/QueryConnection::DefaultChannel=\/${NUMER_KANALU}/" "${DIR_BOTS}/bot_${COUNT_BOTS}/configTS3AudioBot.cfg"
Use a different delimiter around the s parameters:
sed -i "s#QueryConnection::DefaultChannel=.*#QueryConnection::DefaultChannel=/${NUMER_KANALU}#" "${DIR_BOTS}/bot_${COUNT_BOTS}/configTS3AudioBot.cfg"

Related

replacement with sed linux

I need to perform a replacement with sed in linux but it doesn't work.
from [$sonarqubeName] to [$projectName][$branchName]
I tried sed -i 's/[$projectName]/[$projectName][$branchName]/g'
sed -i 's/[$projectName]/[$projectName][$branchName]/g'
The characters [, $, ] have special meaning inside the regular expressions (and some other characters as well, but they are not appearing in your search string). To use them as literal symbols you need to escape them with a backslash in the search expression. Try
sed -i 's/\[\$sonarqubeName\]/[$projectName][$branchName]/g'

Replace a word that has a specific format with another format

I want to replace the space between c and 2 ('C 2',) with two spaces as 'C 2', I tried with sed -i but it does not work
sed -i 's/'C 2',/'C 2',/g' test.dat
The quotes stop the quoting. Either change the quoting or escape it.
You could do it like so:
sed -i 's/'\''C 2'\'',/'\''C 2'\'',/g' test.dat
The '\'' stop the quoting, escape a single quote and then continue with quoting.
But for your specific case, you could just use double quotes:
sed -i "s/'C 2',/'C 2',/g" test.dat

How can I use sed to perform this replacement?

I need to replace a line in file with something
#Banner none
to
Banner /etc/motd
I tried:
sed -i "s/^#Banner none/Banner /etc/motd/" /root/testfile.sh
which is not working. Do you know how I can do what I want?
Since you have slashes in the replacement, you should use other separators than slashes, e.g. commas:
sed -i "s,^#Banner none,Banner /etc/motd/," /root/testfile.sh
Otherwise, you'd have to escape the slashes in the path:
sed -i "s/^#Banner none/Banner \/etc\/motd/"
You'll need to escape your slash or use a different separator than "/".
Could use
sed -i "s/^#Banner none/Banner \/etc\/motd/" /root/testfile.sh
or
sed -i "s,^#Banner none,Banner /etc/motd," /root/testfile.sh

Sed replace ">" to "/>" bash

I'm newbie and I would like to replace "special" caracters with sed. I have an xml file that it is not well formed and at the end of any data row it finish with ">" I need to scrap it and to do it I need to change ">" with "/>". But when I try:
sed -i s/>//>/g FILE
returns => -bash: //: Is a directory
same with:
sed -i s/>/\/>/g FILE
also with
sed -i s,>,\>,g FILE
Man page doesn't solve this problem.
Does anyone face this issue?
You should escape the slash character if you use it a separator:
sed 's/>/\/>/g'
Or use another character e.g.:
sed 's_>_/>_g'
Note that this will replace all matches in the file, not just the last one
If you want to match only > not preceded by /, you can use this:
sed -r 's_[^/]>_/>_g' file
Would sed -i 's/>$//>/g' FILE work for you?
$ echo "<blah> <blah>" | sed 's/>$/\/>/g'
<blah> <blah/>

replace old-link-url to new-link-url with sed

I'm writing a script in bash that would replace old-link-url to new-link-url
my problem is that sed can't replace the url because of the slashes. If i put just some text it works.
my code
sed -e s/"$old_link"/"$new_link"/g wget2.html > playlist.txt
sed supports any character as separator, so if the pattern you are trying to replace contains /, use a different separator. Most commonly used are # and |
sed 's|foo|bar|g' input
sed 's#foo#bar#g' input
Don't forget to put double quotes if you are using variables in sed substitution. Also, if your variable have / then use a different delimiter for sed. You can use _, %, |, # and many more.
So may be something like this would work -
sed -e "s_"$old_link"_"$new_link"_g" wget2.html > playlist.txt

Resources