Daniel
2013-05-08 09:27:41 UTC
I have run into a situation sometimes where literal strings I want to replace that have metacharacters, and I want to replace it with some other similarly odd string.
For example, suppose I want to replace the literal string "jj*hh" with the literal string "foo\1bar".
To make it work, I would have to use escape characters:
$ echo 'jj*hh' | sed 's/jj\*hh/foo\\1bar/'
foo\1bar
I don't mind doing this for one or two strings, but sometimes I have a lot of strings, and don't want to escape them all by hand or write some complex script procedure.
What if the s command could have a f "fixed" flag, similar to the grep -F fixed flag, so the following would work:
$ echo 'jj*hh' | sed 's/jj*hh/foo\1bar/f'
foo\1bar
The f flag would tell s to ignore all metacharacters, to just interpret A and B in s/A/B/f as fixed strings.
Maybe there is some simpler way to do this I am overlooking? Anyway, just a suggestion.
For example, suppose I want to replace the literal string "jj*hh" with the literal string "foo\1bar".
To make it work, I would have to use escape characters:
$ echo 'jj*hh' | sed 's/jj\*hh/foo\\1bar/'
foo\1bar
I don't mind doing this for one or two strings, but sometimes I have a lot of strings, and don't want to escape them all by hand or write some complex script procedure.
What if the s command could have a f "fixed" flag, similar to the grep -F fixed flag, so the following would work:
$ echo 'jj*hh' | sed 's/jj*hh/foo\1bar/f'
foo\1bar
The f flag would tell s to ignore all metacharacters, to just interpret A and B in s/A/B/f as fixed strings.
Maybe there is some simpler way to do this I am overlooking? Anyway, just a suggestion.