Putting shell variables in single quotes disables their interpretation. That's why your command has no effect.
$ echo 's/"$OLD_STRING"/"$NEW_STRING"/g'
s/"$OLD_STRING"/"$NEW_STRING"/g
It should be written like that:
sed -i "s/'$OLD_STRING'/'$NEW_STRING'/g" jboss-beans.xml
But then the variables are interpreted before calling sed
and the again contain special characters:
$ echo "s/'$OLD_STRING'/'$NEW_STRING'/g"
s/'<property name="webServiceHost">${jboss.bind.address}</property>'/'<!--<property name="webServiceHost">${jboss.bind.address}</property>-->'/g
For that reason sed
has this special featur allowing to define the s///
command delimiters by simply using them, e.g.:
sed -i "s#'$OLD_STRING'#'$NEW_STRING'#g" jboss-beans.xml
Still your search expression contains special regexp characters, and using sed
like this is just waste of its abilities. I would write the expression like this:
sed -i 's/\(<.*webServiceHost.*jboss.bind.address.*>\)/<!--\1-->/' jboss-beans.xml
Of course you can make the match string more or less specific according to your needs. There is also other nice feature that can help. sed
allows to narrow editing operations to the lines matching a specific pattern. Your command could look like this:
sed -i '/webServiceHost/ s/^\(.*\)$/<!--\1-->/' jboss-beans.xml
Looking for literal strings with a regular expression, when the search-string contains special characters, is sometimes not as simple as looking for patterns, but you can do it with a bit of juggling.
Note: The echo
command must cater for CMD-special-characters, so it needs ^^
to escape a single ^
and ^|
to escape |
... You don't need CMD's escape-character ^
if you type directly into the file.
Step 1: Create a file, named literal-srch-strings.txt, which containing the exact (unaltered) string to be replaced. There are 2 ways to create this file:
As a command issued at CMD's commandline, or as a command in a .cmd/.bat command-script.
echo sed -i^^/\\*$/$[{" ;"> literal-srch-strings.txt
Make literal-srch-strings.txt yourself, in your text editor.
In this case, you should not use the CMD-escape-character ^
, so the line is has just one ^
, not ^^
-- This is because you are bypassing the CMD-shell.
Here is what is needed in the .txt file (just as the filename says :)
sed -i^/\\*$/$[{" ;"
Step 2: Make a sed script, named str-to-regex.sed , to convert the string(s) into sed regex(s).
Note that the same issue of the CMD-escape-character ^
applies to this step, so again, there are 2 ways you can create the .sed file:
As a command:
echo s/[]\/$*.^^^|[]/\\^&/g; s/.*/s\/^&\/Ciao!\/g/> str-to-regex.sed
Using your text editor, make a file named str-to-regex.sed, containing:
s/[]\/$*.^|[]/\\&/g; s/.*/s\/&\/Ciao!\/g/
Step 3: Run the sed-script which converts the string into a sed regeular expression, and
send its output to another sed-script, replace-text.sed, which will make the actual replacement.
sed -f str-to-regex.sed literal-srch-strings.txt > replace-text.sed
Step 4: Run replace-text.sed -- For the test we can use literal-srch-strings.txt as the input file, but you can, of course, use any input file.
sed -f replace-text.sed literal-srch-strings.txt
Here is the output:
Ciao!
Best Answer
Three things wrong:-
sed
command should be before thes/
option, not beforesed
itself - I presume this is a typing error.>
character has no special meaning in regular expressions, and must not be escaped - the sequence\>
has special significance: it means end of word, and because.*
is "greedy" it matches the end of the last word on the line, hence the retention of the#"
.>
, this will be included in the string to be replaced, so it must also appear in the replacement string.So your edit command should be:
This is similar to jherran's solution, but takes account of your original attempt at matching. It might be neater to match up to the trailing double-quote:
If you don't want to rely on greediness (and make it more readable), use:
Note that any subsequent XML tags on the same line may be deleted by any of the above: to avoid this, use: