Bash one liner to change configuration parameters

I bet there'll be better ones but here's my go:

If the config file has parameters on their own line

sed -i '/ValueTwo/s/= .*/= 22222/' config_file
  • /ValueTwo/ : Search for the string ValueTwo to find which line to operate on (Addresses)
  • s/= .*/= 22222/ : On the lines that match the search above, substitute = .* for = 22222 (Substitute)
  • = .* : Search for the = character followed by a space () character followed by 0 or more of any character (.*) (Regex example)
  • = 22222 : Replace what's found with the literal string = 22222

This will replace the contents of config_file in-place. To create a new file with the parameter changed, remove -i and place > new_file at the end of the line.


If your config file has parameters on the same line (like the unedited question):

sed -i 's/ValueTwo = [^ ]*/ValueTwo = 22222/' config_file

This will replace the contents of config_file in-place as well. It will work as long as there are no spaces in the parameter for ValueTwo. This will also work in the case where parameters are on their own line, but the former method is perhaps more robust in that case.


perl -p -i.bak -e 's/ValueTwo = 2/ValueTwo = 22222/' path/to/configfile

will edit the file in-place and save a copy of the original in case of finger trouble. You can do the same with awk.