How to use sed command to replace folder path names in linux? - bash

How to replace folder path using following command,
sed -i "s/# pidfile: '/var/run/jboss-eap/jboss-eap.pid /# pidfile: '/var/run/jboss-eap/jboss-eap-aa.pid' /g" ks
Error: sed: -e expression #1, char 20: unknown option to `s'
When I use sed -i "s/sample/hi/g" sam.txt It working fine , but above mentioned expression throws unknown exception. How to resolve this error ? have any idea ?

You need to change the delimiter (/) in your statement, as it is also in your pattern.
sed -i "s## pidfile: '/var/run/jboss-eap/jboss-eap.pid ## pidfile: '/var/run/jboss-eap/jboss-eap-aa.pid' #g" ks
Here I replaced it wiht #, but you can use any delimiter, as long as it's not part of the pattern text.

Related

Replace string with special characters value using sed in Ubuntu

Following is the xml file in which STORAGE_ACCOUNT_KEY should be replaced with EoKpaH0W/kqTv9awgIpQX5s+qQwGzXUSxMxhRjfSWG7SIUTWhut1OYQkNxhb3/9UKGf+g4tc3UaC0zKMTSrTNg==
<property>
<name>fs.azure.account.key.storageaccountname.blob.core.windows.net</name>
<value>STORAGE_ACCOUNT_KEY</value>
</property>
I have tried the following. But nothing helps me solve it.
Using / after s and before g
sed -i "s/STORAGE_ACCOUNT_KEY/EoKpaH0W/kqTv9awgIpQX5s+qQwGzXUSxMxhRjfSWG7SIUTWhut1OYQkNxhb3/9UKGf+g4tc3UaC0zKMTSrTNg==/g" test.xml
Error:-
sed: -e expression #1, char 32: unknown option to `s'
Using | after s and before g
sed -i "s|STORAGE_ACCOUNT_KEY/EoKpaH0W/kqTv9awgIpQX5s+qQwGzXUSxMxhRjfSWG7SIUTWhut1OYQkNxhb3/9UKGf+g4tc3UaC0zKMTSrTNg==|g" test.xml
Error:-
sed: -e expression #1, char 112: unterminated `s' command
Note:- $STORAGE_ACCOUNT_KEY is a dynamic variable as below
sed -i "s|STORAGE_ACCOUNT_KEY/$STORAGE_ACCOUNT_KEY|g"
To replace XML, I would adivse to use an XML parser (for example xmllint) instead of sed.
That said, your sed expression is wrong.
sed s command uses 3 delimiters (any printable character you want), but these 3 must be the same and is defined by the one right after the s command.
s/foo/bar/g # right
s|foo|bar|g # right
s|foo/bar|g # wrong
Since you replace a string with a base64 string, you should not use any of the base64 character, so don't use / as a sed delimiter in that case.
Your expression should be like this:
sed -i "s|STORAGE_ACCOUNT_KEY|EoKpaH0W/kqTv9awgIpQX5s+qQwGzXUSxMxhRjfSWG7SIUTWhut1OYQkNxhb3/9UKGf+g4tc3UaC0zKMTSrTNg==|g" test.xml
Note that the g modifier at the end of the command might not be necessary if you only have 1 string to replace per line.

sed command containing variable not working

I can't get the syntax for this command right... I need to change a variable in a file where the variable contains a path.
sessionFolderName=/session_`date '+%y%m%d'`_`date '+%H%M'`
sed "s/sessionFolder=.*/sessionFolder=/"$sessionFolder /home/pi/scripts/settings/settings.sh > tmp
mv tmp /home/pi/scripts/settings/settings.sh
However the result is:
sed: -e expression #1, char 35: unknown option to `s'
# You don't need to (and in your case, should not) invoke date command twice.
# Try running following in bash to see the problem.
# $ echo $(date '+%s')_$(sleep 1)_$(date '+%s')
# On a different note, it's better to use $(...) instead of backticks.
sessionFolderName="/session_$(date '+%y%m%d_%H%M')"
# You can use several other separators in sed.
# e.g. :, ;, #, #, _ and even a space
sed "s:sessionFolder=.*:sessionFolder=${sessionFolder}:" /home/pi/scripts/settings/settings.sh > tmp
mv tmp /home/pi/scripts/settings/settings.sh
Refer to this regarding using $() instead of backticks
The problem is with the / char in the start of your variable. This interrupts sed syntax:
[root# ~]# sed "s/sessionFolder=.*/${sessionFolderName}/" text
sed: -e expression #1, char 21: unknown option to `s'
[root# ~]# echo $sessionFolderName
/session_170824_0942
If you escape it using double backslash - \\, it works:
[root# ~]# sed "s/sessionFolder=.*/sessionFolder=\\${sessionFolderName}/" text
sessionFolder=/session_170824_0942
1) You need to escape the / in your input
2) modify the format of the sed command as shown below
sessionFolderName=\\/session_`date '+%y%m%d'`_`date '+%H%M'`
sed "s/sessionFolder=.*/sessionFolder="$sessionFolderName"/" /home/pi/scripts/settings/settings.sh > tmp

bash sed: -e expression #1, char 21: number option to `s' command may not be zero

I want to rewrite a line in a conf file. The text that should be put on line 88 is: auth 0.0.0.0/0 - -
My command is :
sed -i '88s/.*/auth 0.0.0.0/0 - -/' /etc/opt/ss5/ss5.conf
But I'm getting an error
sed: -e expression #1, char 21: number option to `s' command may not be zero
How can I solve this issue ?
It is because the text you try to inject contains / which is used by sed as a separator.
You need either to escape this slash , or to use a different sed separator. Sed can accept any character as separator, i.e #.
sed -i '88s/.*/auth 0.0.0.0\/0 - -/' /etc/opt/ss5/ss5.conf
#OR
sed -i '88s#.*#auth 0.0.0.0/0 - -#' /etc/opt/ss5/ss5.conf

Find and Replace string using sed gives error

I am using shell script. My requirement is to find and replace the string. The string contains "/" char as well. I am getting error sed: -e expression #1, char 18: unterminated `s' command. Can someone tell how should i replace the string which has "/"?
#!/bin/bash
...
search_string="../conf/TestSystem/Inst1.xml"
rep="Inst1/Instrument.xml"
sed -i 's|${line}|${rep}/g' MasterConfiguration.xml
I tried using another sed command but that one also gave error sed: -e expression #1, char 13: unknown option to `s'
sed -e "s/${line}/${rep}/g" MasterConfiguration.xml > tempfile
Whenever you deal with shell-variables you have to get them out of the "sed-string":
For example:
sed -e "s/"${line}"/"${rep}"/g" MasterConfiguration.xml > tempfile
Otherwise sed will treat the chars as-is and search for ${line} literally:
As you see, nothing happens here.
Furthermore, if your variables contain / you need to use another delimiter for sed. I tend to use ~ in such a case, but you're free to use other chars - just be consequent and don't mix them like in your first example-sed-command:
sed 's~'${line}'~'${rep}'/g' //WRONG
sed 's~'${line}'~'${rep}'~g' //RIGHT
Combine both and it will work:
You can try this sed,
sed -i "s#${line}#${rep}#g" MasterConfiguration.xml
Problem:
Instead you have,
sed -i "s|${line}|${rep}/g" MasterConfiguration.xml
It should be,
sed -i "s|${line}|${rep}|g" MasterConfiguration.xml
Syntax:
sed "s|pattern|replacement|g"

replace double quoted hash with sed

I have a problem replacing default password hash in config file:
sed -i 's/default_password_crypted: "[^"]*"/default_password_crypted: "\$1\$mF86/UHC\$WvcIcXred6crBz2onWxyac."/' input.txt
i get following error:
sed: -e expression #1, char 74: unknown option to `s'
works:
search pattern: default_password_crypted: "$1$mF86/UHC$WvcIcX2t6crBz2onWxyac."
sed -i 's/default_password_crypted: "[^"]*"/default_password_crypted: "1234567890"/' input.txt
how do i need to write replace pattern for hash ?
thx
You need to escape the literal / inside your replacement as it’s the delimiter:
sed -i 's/default_password_crypted: "[^"]*"/default_password_crypted: "\$1\$mF86\/UHC\$WvcIcXred6crBz2onWxyac."/' input.txt
Or simply use a different character, for example a ,:
sed -i 's,default_password_crypted: "[^"]*",default_password_crypted: "\$1\$mF86,UHC\$WvcIcXred6crBz2onWxyac.",' input.txt
You also don’t need to escape the $ inside the replacement.

Resources