how to use sed command properly to replace values containing / delimiter [duplicate] - linux

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 5 years ago.
File: abc.properties
tomcat.home=/opt/tomcat
Set to /usr/local/tomcat. Following cmd is working.
sed -i "/tomcat.home=/ s/=.*/="usr\\/local\\/tomcat"/" abc.properties
Set to $WORKSPACE/tomcat. Following cmd is NOT working since value of the $WORKSPACE is having / delimeters.
sed -i "/tomcat.home=/ s/=.*/="$WORKSPACE\\/tomcat"/" abc.properties
Anyone has an idea how to success above cmd.
Thank you and appreciate your support...

Sed lets you use any character you want as the delimiter. Whatever follows the s is used as the separator:
sed -Ee 's/foo/bar/'
sed -Ee 's|foo|bar|'
sed -Ee 's#foo#bar#'
^- All of those are equivalent.
The other option is to escape all your / as \/, but that gets nightmarish fast. Prefer to just pick a separator character that doesn't collide with characters you're trying to use for something else.

Related

Bash script find and replace string with variable that contains complex special characters without having to escape? [duplicate]

This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 3 months ago.
Is there a simpler way to replace a string with a variable literally that contains all complex characters without needing to do character escaping?
complex='~`!##$%^&*()-_=+\|]}{[;:/?.>,<\/*&\#/'
sed -i "s/secret\ =\ \".*/secret\ =\ \"$complex\"/g" ./file.txt
I already know that & \ and / are the problems. I can do all manner of cleaning with sed commands, but is there a simpler way? Is there a way I can literally make sed read that variable as is?
My work around for now is the following, but even this does not work with / properly ...
complex='~`!##$%^&*()-_=+\|]}{[;:/?.>,<\/*&\#/'
psk_bs="$(echo $complex | sed 's/\\/\\\\\\/g')"
psk_bs_amp="$(echo "$psk_bs" | sed 's/\&/\\&/g')"
psk_bs_amp_fs="$(echo "$psk_bs_amp" | sed 's,'/','\/',g')"
sed -i "s/secret\ =\ \".*/secret\ =\ \"$psk_bs_amp_fs\"/g" ./file.txt
I can do all manner of cleaning with sed commands, but is there a simpler way? Is there a way I can literally make sed read that variable as is?
I'm afraid there is not. But character escaping is not as a big deal as you make it seem to be. This'll work just fine:
psk=$(sed 's/[/&\]/\\&/g' <<< $complex)
sed -i "s/secret = \".*/secret = \"$psk\"/g" ./file.txt
With bash 5.2 you don't even need sed for escaping:
psk=${complex//[\/&\\]/\\&}

How to use sed to replace text with a file path? [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?
Here is my script currently:
currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"
sed -i -e "s/filepath/$filepathvar/g" aimparmstest
When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:
sed: -e expression #1, char 13: unknown option to `s'
Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.
You can use any character as the separator (the first character). For example:
echo "a/b/c" | sed -e 's|/|_|g'
In your case:
sed -i -e "s|filepath|$filepathvar|g" aimparmstest

SED replace a word inside a bracket [duplicate]

This question already has answers here:
What is the difference between `sed -i -e` and `sed -ie`?
(2 answers)
Closed 2 years ago.
I need to change some words in bulk, but because of the brackets inside it, I think I do something wrong.
Line that needs to be changed
echo "CMD_PLUGINS_ADMIN/admin/index.html";
I need to change it to this:
echo "CMD_PLUGINS_ADMIN/reseller/index.html";
I tried it with: sed -ie 's/admin/reseller/' *
But does not change anything, I hope someone knows the right command for it.
$ echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
your input has slash and you are using slash as sed seperator
Either escape the slashes in input by preceeding them with backslash:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
OR change seperator to any other like pipe:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's|admin|reseller|g'
"CMD_PLUGINS_ADMIN/reseller/index.html";

How to get a substring using sed being the pattern "/" [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I have the following string:
/book/A00001/2018/01/15/Chamber_Music
And I want to get using the sed command:
/book/A00001/2018/01/15/
Thanks
Regards
Maybe are you looking for: sed "s/\(.*\)Chamber_Music/\1/g"
No need to use sed, you can use normal shell string handling:
filename='gash.txt'
new_filename="$filename.new"
while read line
do
line=${line%/*}
echo $line
done <"$filename" >"$new_filename"
#mv "$new_filename" "$filename" # Commented out to be optional
Given your input in your second question:
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
You can change the regexp sed delimiter.... see the s command documentation. If you use, e.g.
sed 's:([a-zA-Z0-9/]*)[a-zA-Z]*$:\1:'
then, the / loses its special treatment and the : character assumes it. Of course, you can store the matching pattern in an environment variable, before to feed it to sed(1), and substitute all / into \/ to escape every /.
pattern=`echo "([a-zA-Z0-9/])[a-zA-Z]*\$" | sed 's/\//\\//'`
sed "s/${pattern}/\\1/"
but this is more complex.

Apend the lines in a config file using shell script [duplicate]

This question already has answers here:
How to escape single quote in sed?
(9 answers)
Closed 6 years ago.
I have a config.js file which contents below strings
.constant('Digin_Engine_API', 'http://local.net:1929/')
I want to read this file and replace what ever the things which are there after the .constant('Digin_Engine_API'I tried using sedbut ddnt worked. This is what I used for sed
sed -i 's/^.constant('Digin_Engine_API', .*/http://cloud.lk:8080/' /var/config.js
As a summary my final out put (config.js) file needs to consists below.
Before
.constant('Digin_Engine_API', 'http://local.net:1929/')
After
.constant('Digin_Engine_API', 'http://cloud.lk:8080/')
You need to use double quotes around sed command since single quote is part of pattern
You should use an alternate delimiter since / is used in replacement
You need to capture the first part and use it in replacement
You need to quote the replacement and also add closing )
Sed command:
sed -i.bak "s~\(\.constant('Digin_Engine_API', \).*~\1'http://cloud.lk:8080')~" /var/config.js
cat /var/config.js
.constant('Digin_Engine_API', 'http://cloud.lk:8080')
Here you are:
sed -i -r "s_(\.constant\('Digin_Engine_API').*_\1, <new content>)_" file
Remarks
you cannot use ' in sed command if command is surrounded by '' also,
you must escape all ( and ) that are part or string, and not the sed grouping command,
you must escape . character, because it is also sed replacement for every char,
you must use another sed s separator instead of / if you need to use / in that command, but you can also escape / by \/.

Resources