How do I use sed to change my configuration files, with flexible keys and values?

sed -i -e '/central\.database =/ s/= .*/= new_value/' /path/to/file

Explanation:

  • -i tells sed to save the results to the input file. Without it sed will print the results to stdout.
  • /central\.database =/ matches lines that contain the string between slashes: central.database =. The . is escaped since it’s a special character in regex.
  • The s/OLD/NEW/ part performs a substitution. The OLD string is a regular expression to match and the NEW part is the string to substitute in.
  • In regular expressions, .* means “match anything”. So = .* matches an equal sign, space, and then anything else afterward.

Leave a Comment