Friday, August 14, 2009

use sed to find and replace a particular text

One of my colleague asked me how to find and replace a particular text in a remote shell where X Windows / any IDE is not available. I just googled for sometime and then I tried my self with only one hint to use the sed command. You will not believe I tried it for 5 times and I got the thing working at the 6 attempt, wow i felt so happy. Anyway back to the command, please use the below command to find and replace in any file.

sed -i "s/example/sample/g" *

The above command will find "example" text in all the files present in the current directory and replaces them with "sample" text. The text example / sample not only accepts exact words but regular expression is also possible.

Please feel free to ask me if you need any help regarding this.

No comments:

Post a Comment