Console9

How to remove comments from a config file with sed

Strip comment lines and inline comments from configuration files using sed regex patterns.

How to remove comments from a config file with sed

Strip comment lines and inline comments from configuration files using sed regex patterns.

Prerequisites

  • sed installed.

Step-by-Step: Remove Comments with sed

  1. Remove lines that start with # (full-line comments):

    sed '/^#/d' config.conf
  2. Remove lines that start with # and blank lines:

    sed '/^#/d; /^$/d' config.conf
  3. Remove inline comments (everything after # on a line):

    sed 's/#.*$//' config.conf
  4. Combine all three — remove comment lines, blank lines, and inline comments:

    sed '/^#/d; /^$/d; s/#.*$//' config.conf
  5. Apply in-place:

    sed -i '/^#/d; /^$/d; s/#.*$//' config.conf