Replace text in file with variable using sed

mcExchange

I need to replace a string in a file by another string which is stored in a variable.
Now I know that

sed -i 's|sourceString|destinationString|g' myTextFile.txt

replaces a string but what if destination String was a combination of a hard coded string and a variable?

myString="this/is_an?Example=String"
sed -i 's|sourceString|${myString}destinationString|g' myTextFile.txt

The latter doesn't work, since $myString is not interpreted as a variable.

Oli

Bash doesn't interpret variables in single-quote strings. That's why that isn't working.

myString="this/is_an?Example=String"
sed -i "s|sourceString|${myString}destinationString|g" myTextFile.txt

Would work.

Or if you need the single-quote for another reason, you can just butt strings together and they'll be intepreted as one:

myString="this/is_an?Example=String"
sed -i 's|sourceString|'"$myString"'destinationString|g' myTextFile.txt

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using sed in a Jython script to replace a whole line with a variable in a text file

From Dev

Using sed in terminal to replace text in file

From Dev

Using sed in terminal to replace text in file

From Dev

Replace text in file with SED

From Dev

sed replace a text with text in variable

From Dev

Replace text between 2 particular lines in a text file using sed

From Dev

How to use sed to replace variable value declared in a text file

From Dev

Replace variable in bash file using sed - output lags

From Dev

How to replace multiple occurrences of variable in a file using sed?

From Dev

Using sed to replace text within a java properties file

From Dev

Find and replace text in a file between range of lines using sed

From Dev

shell bash, cat file whilst using sed to replace text

From Dev

multiple file text replace with sed

From Dev

How to replace a text with ":" using sed

From Dev

Using sed to find-and-replace in a text file using strings from another text file

From Dev

How to append text stored in a shell variable in the beginning of the file by using sed?

From Dev

Using sed to replace \ characters in file

From Dev

Replace odd lines in one text file with the corresponding odd lines in another text file using sed/awk/etc

From Dev

Replace string variable with string variable using Sed

From Java

Replace block of text inside a file using the contents of another file using sed

From Dev

Replace newline and variable in text file

From Dev

Bash sed replace text with file content

From Dev

Using SED to uncoment a config file line, and replace text in a Linux config file

From Dev

Append text to file using sed

From Dev

Using SED to delete text in a file

From Dev

Using sed to Replace Environment Variable with directory path

From Dev

Using sed to replace variable name and its value

From Dev

Sed string replace using environment variable

From Dev

replace zero with text using sed or awk

Related Related

  1. 1

    Using sed in a Jython script to replace a whole line with a variable in a text file

  2. 2

    Using sed in terminal to replace text in file

  3. 3

    Using sed in terminal to replace text in file

  4. 4

    Replace text in file with SED

  5. 5

    sed replace a text with text in variable

  6. 6

    Replace text between 2 particular lines in a text file using sed

  7. 7

    How to use sed to replace variable value declared in a text file

  8. 8

    Replace variable in bash file using sed - output lags

  9. 9

    How to replace multiple occurrences of variable in a file using sed?

  10. 10

    Using sed to replace text within a java properties file

  11. 11

    Find and replace text in a file between range of lines using sed

  12. 12

    shell bash, cat file whilst using sed to replace text

  13. 13

    multiple file text replace with sed

  14. 14

    How to replace a text with ":" using sed

  15. 15

    Using sed to find-and-replace in a text file using strings from another text file

  16. 16

    How to append text stored in a shell variable in the beginning of the file by using sed?

  17. 17

    Using sed to replace \ characters in file

  18. 18

    Replace odd lines in one text file with the corresponding odd lines in another text file using sed/awk/etc

  19. 19

    Replace string variable with string variable using Sed

  20. 20

    Replace block of text inside a file using the contents of another file using sed

  21. 21

    Replace newline and variable in text file

  22. 22

    Bash sed replace text with file content

  23. 23

    Using SED to uncoment a config file line, and replace text in a Linux config file

  24. 24

    Append text to file using sed

  25. 25

    Using SED to delete text in a file

  26. 26

    Using sed to Replace Environment Variable with directory path

  27. 27

    Using sed to replace variable name and its value

  28. 28

    Sed string replace using environment variable

  29. 29

    replace zero with text using sed or awk

HotTag

Archive