SKPS
SKPS

Reputation: 5836

Bash : Search and replace value of a variable inside a file

I have a input-file for a code, which has a parameter called var1. I need to run a bash script to read and change var1 inside the input-file and submit to the code.

Inside the input-file:

...
var1=10
...

I can use grep to get value of var1. But how to replace and write var1 inside the input-file?


Command I use to obtain var1

INPUT=input-file
var1=`grep "^var1" $INPUT | awk '{print $3}' | sed "s/'//g"`

Upvotes: 0

Views: 3266

Answers (2)

user112333
user112333

Reputation: 75

This worked for me:

sed  -i -e 's/\(^var1=\).*/\1Newvalue/' $INPUT

Upvotes: 0

Kent
Kent

Reputation: 195059

grep doesn't do any substitution. You could consider using sed or awk ... For example:

sed  -i 's/\(^var1=\).*/\1Newvalue/' $INPUT

will change the value to Newvalue

Upvotes: 3

Related Questions