I don’t know about the other people but to me the scariest UNIX command is by far sed. Today I found myself in a situation where I found no other way to solve my problem but to use sed… and it worked… and was not all that scary :)

I needed to replace a bunch of strings in a bunch of files in a bunch of subdirectories. Yo know… the kind of fun people are having when moving around and re-organizing things that depend on tons of config files…

So I had to replace all strings which start with ../ to ../../ – in other words I was moving one of my directories deeper into the tree and now to access some paths which were 1 level above we need to go 2 levels up.

I tried the standard ‘replace’ command, some gui tool, some bash voodoo of my own and nothing worked.
Finally I resorted to sed and the line that did the job for me was:

$ find ./ -type f -exec sed -i -r s'/^\.\.\//..\/..\//' {} \;

The GREEN part is the “from” string, the BLUE part is the “to” string and the RED chars are part of the sed ‘search’ command syntax…

Tagged with:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>