Monday, January 3, 2011

Script to replace a string with another string in a file.

Lets assume that the file name is test. The file test contains many lines and you want to replace a line with another line.

1) Make a file abc,sh.
$ touch abc.sh

2) Insert the script into abc,sh.
$ vi abc.sh press i, then paste

#/bin/bash
cat filename
echo "enter the newstring"
read new
echo "enter the oldstring"
read old
sed -i "s/$old/$new/g" test

3) sh abc.sh

Output:
------
enter the newstring
new
enter the oldstring
old
------

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews