0

I realize similar questions have been asked previously, many, times. However, still getting errors. I have the string

dom0Size = ${/ 2E-6 ${position_units}}

That I want to change to

dom0Size = ${/ ${d}E-6 ${position_units}}

I have used sed for similar tasks that are not so special character heavy. I tried

sed -i "s?dom0Size = \$\{\/ 2E-6 \$\{position_units\} \}?dom0Size = \$\{\/ 2E-6  \$\{position_units\} \}?g" "test.txt"

but even with all these escape slashes, I get the error

sed: -e expression #1, char 53: Invalid content of \{\}

Any advice?

Community
  • 1
  • 1
user1543042
  • 3,422
  • 1
  • 17
  • 31
  • If I may, when you look in your file, is it correct to say that every single character on the line presented is needed to make it unique? ie. there may exist another line in the file that has the following :- dom0Size = ${/ 3E-6 ${position_units}} ... So here we see that only the '3' makes the line different. – grail Mar 18 '17 at 08:12
  • the important part is the `dom0Size = ${/ ${d}E-6`, but with all the special characters, I just put in the complete line – user1543042 Mar 18 '17 at 15:38

2 Answers2

1

You can use the following :

sed -i 's?dom0Size = ${/ 2E-6 ${position_units}}?dom0Size = ${/ '${d}'E-6 ${position_units}}?g' test.txt

It uses multiple single-quotes parts to avoid a bothersome handling of the special characters, combined with the variable you need expanded. Take care not to add any space between the quoted parts and the variable or they will be parsed by bash as multiple tokens.

Aaron
  • 24,009
  • 2
  • 33
  • 57
-1

If you know that there is only a single reference to dom0Size, you could do something like:

sed -r -i.bak "/dom0Size/s/[0-9]+/$d/2" file

This would minimise the need to worry about all the other special characters on the line. As mentioned in my comment, it depends on what makes the line unique?

grail
  • 914
  • 6
  • 14