Change variable value inside text file with Sed
18,807
Solution 1
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
Solution 2
In case you want more complex substitution:
sed_param=s/Var1=.*/Var1=${NEW_VALUE}/
sed -i "$sed_param" file.txt
Comments
-
HobbitOfShire almost 2 years
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?
-
Olie over 9 yearsSuppose I want to set it to
${NEW_VALUE}
? I can't use${var}
inside of single quotes and, if I switch to double-quotes, the.*
part doesn't work (does it?!) So now what? -
Alberto Salvia Novella almost 3 yearsYou can use double quotes.