How can I use sed to perform this replacement? - linux

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

Related

How to find and replace a file path (along with special characters) using SED command

How to find and replace a file path like a/b/c/d into x/y/z using sed command?
I tried sed -i -e 's/a/\b/\c/\d/x/\y/\z' file.txt but it did not work.
Just use another separator for s command. You can use any character. Just / is the most popular one. I like # or #. Then you have to escape that character.
sed 's#a/b/c/d#x/y/z#'
but it did not work
To escape / use \/ not /\. The \ needs to be in front of /. You could:
sed 's/a\/b\/c\/d/x\/y\/z/'

writing a special character in sed bash

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"

Add many quotation in sed instruction

I have to update line with value containg many quotation characters
sed -i 's/.*sonar.links.issue.*/property "sonar.links.issue", "http://jra.url"'
I get error there. What should i do to do it correctly?
Your replacement string contains slashes "http://", so you shouldn't use / as the delimiter for this sed command, use # as shellter proposes.
To complete its answer, I would add the "g" flag, at the end of the sed expression, to ensure the subsitution is made on the whole line, and not only on the first match with ".sonar.links.issue.".
sed -i 's#.*sonar.links.issue.*#property "sonar.links.issue", "http://jra.url"#g' file
Try
sed -i 's#.*sonar.links.issue.*#property "sonar.links.issue", "http://jra.url"#' file
IHTH

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