I want to do some simple string replace in Bash with sed. I am Ubuntu 10.10.
Just see the following code, it is self-explanatory:
name="A%20Google.."
echo $name|sed 's/\%20/_/'|sed 's/\.+/_/'
I want to get A_Google_ but I get A_Google..
The sed 's/\.+/_/' part is obviously wrong.
BTW, sed 's/\%20/_/' and sed 's/%20/_/' both work. Which is better?
sedspeaks POSIX basic regular expressions, which don’t include+as a metacharacter. Portably, rewrite to use*:or if all you will ever care about is Linux, you can use various GNU-isms:
That last example answers your other question: a character which is literal when used as is may take on a special meaning when backslashed, and even though at the moment
%doesn’t have a special meaning when backslashed, future-proofing means not assuming that\%is safe.Additional note: you don’t need two separate
sedcommands in the pipeline there.(Also, do you only need to do that once per line, or for all occurrences? You may want the
/gmodifier.)