Bash one liner to change configuration parameters

I have a config file with following structure.

ValueOne = 1
ValueTwo = 2
ValueThree = 3

I want a one liner bash script to find ValueTwo and change the value to 22222.

Any idea?
Not School Thing

Asked By: TRA

||
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.

Answered By: RedGrittyBrick

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.

Answered By: drs

Assuming ValueTwo is a number, sed will do just fine:

sed -e 's/ValueTwo = [0-9]*/ValueTwo = 2222/g' your_config_file > output_file
Answered By: PaweĊ‚ Rumian

I’d go for awk:

awk '/ValueTwo/{$3=22222}1;' file > newfile

The above checks whether a given line matches ValueTwo and sets the 3d field to 222 on matching lines. The 1; is just an awk shgorthand way of writing print $0, it will print each line. Since it is outside the match block (/ValueTwo/{}), it will cause all lines to be printed.

Since you asked for a bash solution though (don’t know why you would prefer one but still), you could try this:

while read key eq val; do
    [ $key = "ValueTwo" ] && val=22222
    printf "%s %s %sn" $key $eq $val
done <  file
Answered By: terdon

Using ex editor (part of Vi):

ex +'%s/^ValueTwo[^=]?=[^0-9]?zs[^$]+/22222/' -scwq config.ini

Explanation:

  • +cmd – executes ex/vi command;
  • %s/foo/bar/search and replace syntax;
  • ^ – beginning of the line;
  • [^=]?=[^0-9]? – selects area around equal character (=);
  • zs[^$] – selects part for the replacement till end of the line;
  • /22222/ – pattern to replace to (everything after zs);
  • -ssilent mode;
  • -c<command> – executes <command>;
  • -cwq – executes write and quit;
Answered By: kenorb
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.