June 14, 2021 | 11:10
Reading-Time: ca. 2 Min

Simple Configcleaner

The simple, unspectacular things in life bring you ahead the curve. This bash script, for instance, removes all comments from a configuration file. In order that no empty lines remain instead, these are subsequently removed afterwards. What remains is the essence: what really counts.

The usage is straightforward: Just add the desired config file(s) as parameters and you’re done!

#!/bin/bash

# Beseitigt alle Kommentar- und Leerzeilen aus einer Configfile
# Sichert Datei mit Erweiterung .backup und
# erwartet Dateiname oder -liste als Parameter

for FILE in "$@"
 do

  # Erstellt Sicherungskopie(n)
  cp --backup=numbered $FILE $FILE.backup

  # Entfernt alle Kommentarzeilen > Arbeitsdatei
  sed -e 's/#.*$//' $FILE > $FILE.tmp

  # Entfernt alle Leerzeilen aus Arbeitsdatei
  sed -i '/^$/d' $FILE.tmp

  # Ersetzt Ausgangsdatei mit Arbeitsdatei
  mv $FILE.tmp $FILE

 done;

This simplifies automation and above all the search in page-long comments within a config file. However, be careful! Behind every line theres much of wisdom: Everything after a # sign is mercilessly removed, really everything! Take the following line as example:

test = "https://link#something"

this will become

test = "https://link

The syntax (missing closing quotation marks) as well as the content itself are no longer correct and lead to errors. Therefore I always enforce an automatic backup of the original. Unfortunately, fine-tuning the RegEx is not helpfully either, due to the fact that comments often indented with spaces or tabs or come after a command.

Know your tools!

Stay Healthy!
Tomas Jakobs

© 2024 Tomas Jakobs - Imprint and Legal Notice

Support this blog - Donate a Coffee