-2

For some reason, the answer in the post below doesn't work for me. Any thoughts?
how to use sed to replace a string in a file with a shell variable

I'm running CentOS 6.5

`NEW="new value"
cat myfile.txt | sed -e 's/old/${NEW}' <-- just replaces 'old' with '${NEW}'
cat myfile.txt | sed -e 's/old/$NEW' <-- just replaces 'old' with '$NEW'
cat myfile.txt | sed -e "s/old/${NEW}" <-- gives the error: unknown option to `s'
Adoyt
  • 395
  • 2
  • 4
  • 17
  • 2
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Oct 11 '17 at 17:17
  • 1
    Also sed takes filenames as args, no need for cat. Also `s///` requires three delimiters – 123 Oct 11 '17 at 17:18
  • Thank you. I was using cat just as a way to test this without modifying the file. – Adoyt Oct 11 '17 at 18:58

1 Answers1

0

try taking the 's off the sed e.g

$ new=N
$ cat > j
one
two
three
$ sed -e "s/one/${new}/" j
N
two
three

for a more complete answer try this answer

123
  • 10,778
  • 2
  • 22
  • 45
kusala9
  • 16
  • 1
  • Thanks! wrapping the shell variable in double-quotes worked! Like so: sed -e 's/old/'"$NEW"'/' – Adoyt Oct 11 '17 at 18:56