1

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?

8 Answers 8

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure this will work? I though sed only took "Basic" PCRE, meaning the \s character class isn't valid and the parentheses need to be backslash-escaped.
@djhaskin987 Of course it does work: -r, --regexp-extended use extended regular expressions in the script.
To make changes to file you should add -i option to sed.
1

Through sed,

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

Comments

1

with awk

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

Comments

1

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)

Comments

1

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"

Comments

0

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.

Comments

0
$ 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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.