Replace variable value in file with bash/sed/awk

sort of common question but couldn't find a solution around.

I have a source file (python) with:

MYVARIABLE = 123
OTHERVAR = 23
print str(MYVARIABLE)

I want to modify the file by changing the value of MYVARIABLE to 456 from a remote terminal, without recurring to text editing, just a commandline oneliner.

I guess sed could do the job by looking for the first line where MYVARIABLE occurs, deleting everything and replacing with MYVARIABLE = 456

Not sure how this could be done, though. Inputs?

Upvotes: 1

Views: 4577

Answers (8)

konsolebox
konsolebox

Reputation: 75458

Yet another sed:

sed -r 's|^(MYVARIABLE\s*=\s*).*|\1456|' file

Edit: Just like the other answers here, -i can be added as an option so the file can be modified in-place.

Upvotes: 3

ephemerr
ephemerr

Reputation: 1983

This solution limits changes by given section which is usual for ini files.

sed -ri '/'$SECTION'/,/\[/ s/(\s*'$KEY'\s*=\s*).+/\1'$VALUE'/' "$INI_FILE"

Upvotes: 1

sca
sca

Reputation: 39

Using sed, we can split a statement into two - and replace the second part with the required variable [here, 456].

sed -i "s#\(.*MYVARIABLE =\)\( .*\)#\1 "456"#" file

Upvotes: 0

Sylvain Leroux
Sylvain Leroux

Reputation: 51990

Assuming your sed version support character class, you might write that:

sh$ sed -r 's/([[:blank:]]*MYVARIABLE[[:blank:]]*=[[:blank:]]*)[[:digit:]]+/\1456/' file.py
MYVARIABLE = 456
OTHERVAR = 23
print str(MYVARIABLE)

Or

sh$ sed -r 's/(\s*MYVARIABLE\s*=\s*)[0-9]+/\1456/' file.py
MYVARIABLE = 456
OTHERVAR = 23
print str(MYVARIABLE)

Both will preserve spaces before variable definition. This is important as indentation matters in Python.

Please note however this is rather fragile. Some valid python variable declaration might not be properly modified by that simple regex.


If you're confident enough to replace "in place", add the -i option available on some implementations of sed.

From man sed:

   -i[SUFFIX], --in-place[=SUFFIX]

          edit files in place (makes backup if extension supplied)

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203189

$ awk -v var="MYVARIABLE" -v val="456" '$1==var{$NF=val} 1' file
MYVARIABLE = 456
OTHERVAR = 23
print str(MYVARIABLE)

$ awk -v var="OTHERVAR" -v val="123456" '$1==var{$NF=val} 1' file
MYVARIABLE = 123
OTHERVAR = 123456
print str(MYVARIABLE)

Upvotes: 0

Jotne
Jotne

Reputation: 41446

This simple awk should do:

awk '/^MYVARIABLE/ {$3="456"}1' file
MYVARIABLE = 456
OTHERVAR = 23
print str(MYVARIABLE)

If line starts with MYVARIABLE, change 3rd field to 456
The 1 prints all lines.

Upvotes: 0

jrjc
jrjc

Reputation: 21873

with awk

awk '{if (/MYVARIABLE = [0-9]*/){print "MYVARIABLE = 456"}else{print $0}}' file
MYVARIABLE = 456
OTHERVAR = 23
print str(MYVARIABLE)

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

Through sed,

$ sed '/^MYVARIABLE/s/^\(.*=\s*\).*$/\1456/' file
MYVARIABLE = 456
OTHERVAR = 23
print str(MYVARIABLE)

Upvotes: 1

Related Questions