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.

5
  • Are you actually using this quoting: sed -i -e 's/$a/$b/g", open with single quote and close with double quotes?
    – user232326
    Sep 24, 2016 at 6: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, 2016 at 6: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.
    – user232326
    Sep 24, 2016 at 6:49
  • 1
    Possible duplicate of How can I use variables when doing a sed? Sep 24, 2016 at 13:30
  • The question was clear enough, and a duplicate - but not that one. Sep 24, 2016 at 14:01

1 Answer 1

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

Not the answer you're looking for? Browse other questions tagged .