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

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

Related

Find and replace string with "/" recursively in a directories in RHEL 7.4

I have to find and replace all the occurrence of a string in all files /eOffice/eofficev6 to /eOffice/SAPS/eofficev6 recursively in a directory in RHEL 7.4.
Problem is that I using sed -i but my string also containing / slash.
How to replace all string having /?
You have two ways to achieve what you want.
One: Escape slashes (E.G. sed -i "s/\/eOffice\/eofficev6/\/eOffice\/SAPS\/eofficev6/" file).
Two: change the delimiter (E.G. sed -i "s|/eOffice/eofficev6|/eOffice/SAPS/eofficev6|" file).
You could do
sed -i 's/\(\/eOffice\)\(\/eofficev6\)/\1\/SAPS\2/' input_file_name
The "/eOffice" and "eofficev6" parts are grouped and the "SAPS" is inserted in between.
For example, if the input is:
/eOffice/eofficev6
the output will be
/eOffice/SAPS/eofficev6
Forward slashes are escaped with \s.
Or without grouping just,
sed -i 's/\/eOffice\/eOfficev6/\/eOffice\/SAPS\/eOfficeb6/' input_file

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

How to keep "/" in string while using sed

packageUrl="http://192.168.0.112"
packageUrl_line=`grep -n "packageUrl" ${file} | head -1 | cut -d ":" -f 1;`
sed -ig "${packageUrl_line},${packageUrl_line}s/\"packageUrl\".*/\"packageUrl\":\"${packageUrl}\",/g" ${file}
As code, it throws an error, because packageUrl contain "/", how to resolve this problem without modify packageUrl?
sed: 1: "2,2s/"packageUrl":.*/"p ...": bad flag in substitute command: '/'
You don't need to use / as your sed s delimiter. It's happy with most characters:
sed 's,bob,tony,'
for example.
Try to escape the literal /:
packageUrl="http:\/\/192.168.0.112"
Your shell may support substitution in parameter expansion. It looks a bit cryptic with \ and /, but in Bash:
$ echo "$packageUrl ${packageUrl//\//\\/}"
http://192.168.0.112 http:\/\/192.168.0.112
The leaning toothpicks //\//\\/ breaks up as follows:
// replace all
\/ occurrences of the single character /
/ with
\\/ the two characters \ and /
If you use a different delimiter for sed's s command, you get a more readable substitution. For example, using # as delimiter, you can write ${packageUrl//#/\\#}.
If you can pick a delimiter that you are absolutely sure won't be present in $packageUrl, then you can skip the substitution entirely - but be sure that you're sure!

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