Replace a string with an environment variable.

Tuesday, April 30, 2013 2 Comments

Many times its required inside a shell script that you need to replace a string in a file with a value which is present in a environment variable.Most of the time people will face this in their daily work life if they work on unix exclusively.
Below is the way to do it.
echo $LINE | sed -e "s/12345678/${replace}/g"
Small test below:
export replace=987654321
echo X123456789X | sed "s/123456789/${replace}/"
X987654321X

2 comments:

  1. nice post Vijay...Could you tell me how to write a shell script to search for a variable in another file and increment its value by 1. I tried something like this:

    sed -i "s/$var1/$((var+1))/g" filepath/filename

    which is not working

    ReplyDelete