Replace string by sed with double quota (special characters) - linux

I want to change fileSystem: "/test/test" to fileSystem: "/apptest" in RHEL6 and i tried to use :
sed -i 's/fileSystem: "/test/test"/fileSystem: "/apptest"' text.txt
but doesnt work, output :
sed: -e expression #1, char 26: unknown option to `s'
I have no idea how to change it. I read some documents but didnt find out how to work.
Can you help me?
Regards.

Because your string itself contains / characters, you should use a different delimiter for the s command. For example, #:
sed -i 's#fileSystem: "/test/test"#fileSystem: "/apptest"#' text.txt
(You were also missing the delimiter after the replacement.)

Related

How to set sed command correctly

When I try following command, I'd like to rewrite sql.
Day='2020/12/1'
Dir=/home/test/data
sql=`cat $Dir"/"$test".sql" | sed -e "s/Day/$Day/g"`
I suffered following errors.
sed: -e expression #1, char 24: unknown option to `s'
Why the s is recognised as option ? why is this command couldnt work well ?
if someone has opinoin, please let me know
Thanks
The problem is with slashes: your variable contains them and the final command will be something like sed "s/string/path/to/something/g", containing way too many slashes.
Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string like below:
sql=`cat $Dir"/"$test".sql" | sed -e "s|Day|$Day|g"`
Also, you would need to use sed -i to update the file in-place, since it looks like that is what you're trying to do.

How do I find and replace text using sed using ~ as delimiter

Good-day,
In a Bash shell script I'm putting together, I am trying to find this text: /usr/local/freeswitch/log/freeswitch.log and replace it with: /var/log/freeswitch/freeswitch.log in this file: /etc/fail2ban/jail.local
This is what I have tried so far, both of which result in the error: sed: -e expression #1, char 75: unterminated `s' command
Attempt #1
sed -i 's~usr/local/freeswitch/log/freeswitch.log~var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local
Attempt #2
sed -i 's~usr/local/freeswitch/log/freeswitch.log/var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local
My research shows that since the text I'm searching for includes the "/" character, I should be using a different delimiter "~" to separate the find and replace strings. But looks like I'm doing something wrong, any assistance would be appreciated, thanks.
The structure of a sed substitution command is s/PATTERN/REPLACEMENT/ (note the delimiter at the end of the command).
You're right, you can change the delimiter to a different character, so if you're going to use ~ you need to put one of those at the end of the command.

Unterminated `s' command with sed troubleshooting

I have a problem with sed. I want to replace the entire specific line number for multiple lines in multiples documents.
This the bash command for 1 specific line in 1 specific document:
BNAME=$(basename $FILE .pdb)
psfgen1="pdb ./sedpdb/${BNAME}.pdb/"
sed -i '8s/'.*'/'${psfgen1}'/' ./psfgen.inp
And I get this error :
sed: -e expression #1, char 60: unterminated `s' command
Is anyone know how to solve this issue? Thanks!
I can see two things wrong:
There are forward slashes in the string that you're attempting to use in the sed command. These will be interpreted as part of the command, so you should use a different delimiter.
The * is unquoted, so will be glob-expanded by the shell to the names of all the files in the directory.
Reliably using shell variables in string substitutions is non-trivial but can be done using one of the approaches shown in the answers to this question.
In your case, it looks like you can probably get away with using another character as the delimiter, such as #:
sed -i "8s#.*#${psfgen1}#" ./psfgen.inp

SED replacement in a set of files in Linux

I have a following requirement
String 1 : http://gaa-dev.gk.gbcd:9701/analytics
String 2 : https://gaa-prod.gk.gbvd/analytics
There were around 40 different locations in various files(xml content) under /home/gaauser
I want String1 to be REPLACED BY String2
Tried SED, but no luck and I am struck now. Any inputs greatly appreciated.
thanks
Two fixes are needed:
Since you have / in your strings, it is recommended to use a different delimiter or you need to tell sed that it is not a delimiter by escaping them.
Since you are using variables, use double quotes to allow them to interpolate.
str1='http://gaa-dev.gk.gbcd:9701/analytics'
str2='https://gaa-prod.gk.gbvd/analytics'
echo 'http://gaa-dev.gk.gbcd:9701/analytics' | sed "s/$str1/$str2/"
sed: -e expression #1, char 11: unknown option to `s'
echo 'http://gaa-dev.gk.gbcd:9701/analytics' | sed "s|$str1|$str2|"
https://gaa-prod.gk.gbvd/analytics

SED command giving error while replacing a string

I need to replace a string with another string in a file.
i have the below line a file.
tibco.env.LD_LIBRARY_PATH %TPCL_HOME%/lib/httpclient_3.0:%TPC
L_HOME%/lib:%RV_HOME%/lib:%TRA_HOME%/icjava/6.2/lib:%LD_LIBRARY_PATH%
i need to replace "tibco.env.LD_LIBRARY_PATH " with
"tibco.env.LD_LIBRARY_PATH %RV_HOME%/lib/64:"
and finally the line should be
tibco.env.LD_LIBRARY_PATH %RV_HOME%/lib/64:%TPCL_HOME%/lib/httpclient_3.0:%TPC
L_HOME%/lib:%RV_HOME%/lib:%TRA_HOME%/icjava/6.2/lib:%LD_LIBRARY_PATH%
i tried with the below command but getting error sed: -e expression #1, char 66: unknown option to `s'
sed -i s/"tibco.env.LD_LIBRARY_PATH "/"tibco.env.LD_LIBRARY_PATH %RV_HOME%/lib/64:"/ bwengine.tra
can someone help in fixing the issue.
If the pattern you want to replace or the replacement string contain the slash, use a different separator, e.g. =:
s="tibco.env.LD_LIBRARY_PATH "="tibco.env.LD_LIBRARY_PATH %RV_HOME%/lib/64:=
you may think to use & as the matched string which will reduce the length of the sed command.
sed 's!tibco.env.LD_LIBRARY_PATH !&%RV_HOME%/lib/64:!' file
refer this url: http://www.grymoire.com/unix/Sed.html#uh-3

Resources