3

I have a text file containting this

Var1=ofzer
Var2=smelf
..
..
VarN=mskfm

I want to change the value of one of my variables using Sed. How is that possible?

HobbitOfShire
  • 2,144
  • 5
  • 24
  • 41
  • Try searching on this site; you'll surely find examples. – devnull Mar 06 '14 at 15:53
  • Have a look at http://www.grymoire.com/Unix/Sed.html#uh-1 - this is a simple example of substitution using sed. – Josh Jolly Mar 06 '14 at 15:58
  • You'll need to use a regular expression as an *address* to find the line to change. Then use the *"s" command* to change the value. Use the `-i` option to edit the file in-place. Here's the [GNU sed manual](https://www.gnu.org/software/sed/manual/sed.html) – glenn jackman Mar 06 '14 at 15:59
  • possible duplicate of [sed scripting - environment variable substitution](http://stackoverflow.com/questions/584894/sed-scripting-environment-variable-substitution) – tripleee Nov 24 '14 at 06:27

2 Answers2

18

Say you wanted to change the value of Var2 from 'smelf' to 'smurf', you could use:

bash$ sed -i 's/Var2=.*/Var2=smurf/' file.txt
problemPotato
  • 589
  • 3
  • 8
2

In case you want more complex substitution:

sed_param=s/Var1=.*/Var1=${NEW_VALUE}/  
sed -i "$sed_param" file.txt
andrey
  • 588
  • 1
  • 8
  • 14