Sed Disable Regex

This started like a "small" post on this matter, but eventually it became a very long one, and finally decided to split the argument in two. This is about the problem I had regarding regex. You can find a "more in depth" post here:  BigBrus' Corner - Sed Tips and Tricks . 

sed is a command line tool. Of course, I'm no expert of sed, I use it mostly to replace parts of configuration files in my installation scripts. It's very useful for its ability to use regex and to replace in the same file part of lines, but in fact it is almost a programming language, with so many options to manipulate strings you won't believe.

Anyway, the ability to use regex can become a problem when you have strings with lots of special characters. Like, for example, a password or a web address.

SIMPLE ANSWER
Well, the really simple answer is: sed cannot run without regex. There's no parameter, no switch, nothing that can turn off regular expression matching. There may be other tools (I found something with perl, something with pure bash, a partial solution with grep and a "replace" tool, which should come installed with MySQL or something similar...), but not sed.

LONG ANSWER
Anyway, most of the times, the problem isn't with regular expression but with bash escape character - which is \ . Incidentally, it's also the most used delimiter of sed (delimiter = a character chosen to separate the different fields of sed).
For example, if you have to correctly replace "http://127.0.0.1:8080/webinterface" with "http://192.168.50.13:9091/my/web/interface", your sed command will be something like

sed -i 's/http:\/\/127\.0\.0\.1:8080\/webinterface/http:\/\/192\.168\.50\.13:9091\/my\/web\/interface/g' /home/bigbrus_corner/test_sed 

which may be fascinating to someone but in my opinion is madness in a string. By the way, this pattern is called 'Picket Fence'.
The most beautiful option of sed is that you can set its separator to almost any character. And in the most simple way: using it. sed will simply understand.

So, the command above can become

sed -i 's|http://127.0.0.1:9091/webinterface|http://192.168.50.13:9091/my/web/interface|g' /home/bigbrus_corner/test_sed 

or even

sed -i 's#http://127.0.0.1:9091/webinterface#http://192.168.50.13:9091/my/web/interface#g' /home/bigbrus_corner/test_sed 

or even

sed -i 'sshttp://127.0.0.1:8080/webinterfaceshttp://192.168.50.13:9091/my/web/interfacesg' /home/bigbrus_corner/test_sed
(it DOES work this last one! I couldn't believe!! you can use s as a separator)

You can use really ANY character! You only have to put it in the right place and eventually escape any matching character in the string. This example, using i as separator, could make it clearer:

sed -i 'sihttp://127.0.0.1:8080/web\interfaceihttp://192.168.50.13:9091/my/web/\interfaceig' /home/bigbrus_corner/test_sed

Note that, having used i as a separator, I had to escape every other i in the text to have it work. Anyway, that's the reason you usually use "strange" characters like @#!/: or similar...

So, as you can see, probably most of the times (with small portions of not too complicated text) you actually don't need to turn off regex. 

USEFUL LINKS

Labels: , ,