Usually I find my answers in the previously answered questions, but not this time (or I don't understand the answers and can't modify them to do what I want them to do), so I'm asking my first question here.
My input files look like
# -*- coding: utf-8 -*-
[attachment]
[browser]
[changeset]
[components]
tracopt.versioncontrol.svn.svn_fs.subversionconnector = enabled
tracopt.versioncontrol.svn.svn_prop.subversionmergepropertydiffrenderer = enabled
tracopt.versioncontrol.svn.svn_prop.subversionmergepropertyrenderer = enabled
tracopt.versioncontrol.svn.svn_prop.subversionpropertyrenderer = enabled
[header_logo]
[inherit]
file = /etc/trac/trac.ini
[logging]
I'd like to remove all empty sections like attachment, browser, changeset, header_logo and logging. I'd only keep the sections that aren't empty. The output file should look like
# -*- coding: utf-8 -*-
[components]
tracopt.versioncontrol.svn.svn_fs.subversionconnector = enabled
tracopt.versioncontrol.svn.svn_prop.subversionmergepropertydiffrenderer = enabled
tracopt.versioncontrol.svn.svn_prop.subversionmergepropertyrenderer = enabled
tracopt.versioncontrol.svn.svn_prop.subversionpropertyrenderer = enabled
[inherit]
file = /etc/trac/trac.ini
This should happen in a bash script. I've thought of using sed : looking for a regex \[.+\]\n(\n)+(?=\[)
but this doesn't seem to work with sed because I should know in advance how many lines the regex will be and use N
accordingly. The regex should also work with EOF instead of the final \[
, but I can probably do this if I find the way to do it for the \[
.
Any idea how I could do this? Is there a better way than sed?
[sections]
anda = b
key-value pairs. I would always use a tool that understands the that format and use that to modify the file. That way e.g. line wrapping is more likely to be handled correctly. (Python has its configobject library, I assume that perl and ruby have similar standard libraries).