1

I am using this sed command in particular file, but it doesn't work.

e.g.

a="adsfkndsa%/{}==admfkdsa"  
b=sadkfjdsaklfj  

sed -i -e 's/$a/$b/g' /tmp/sample.txt

Please suggest another way.

Ketan Maheshwari
  • 9,054
  • 6
  • 40
  • 53
user191625
  • 11
  • 3
  • Are you actually using this quoting: `sed -i -e 's/$a/$b/g"`, open with single quote and close with double quotes? –  Sep 24 '16 at 06:33
  • Ya i am try both sed -i -e 's/$a/$b/g' & sed -i -e "s/$a/$b/g" but it is not working – user191625 Sep 24 '16 at 06:39
  • Could you post an small part of the file that you want to modify? A part that include the string that you want to change. –  Sep 24 '16 at 06:49
  • 1
    Possible duplicate of [How can I use variables when doing a sed?](http://unix.stackexchange.com/questions/69112/how-can-i-use-variables-when-doing-a-sed) – Eric Renouf Sep 24 '16 at 13:30
  • The question was clear enough, and a duplicate - but not that one. – Thomas Dickey Sep 24 '16 at 14:01

1 Answers1

3

I just tried this, and it works on my system (Debian 8):

sed -i.bak "s|$a|$b|g" /tmp/sample.txt

You need to change the delimiter, since the variable $a contains a /.

Before:

cat /tmp/sample.txt.bak 
foo
adsfkndsa%/{}==admfkdsa
bar

After:

cat /tmp/sample.txt
foo
sadkfjdsaklfj
bar

Edit, sed version info:

sed --version
sed (GNU sed) 4.2.2
maulinglawns
  • 8,426
  • 2
  • 28
  • 36