Files like ~/.config/vlc/vlcrc are 99% junk if you want to version control only the configuration options. I've got a script to remove the comments, but there's a ton of empty configuration sections left over. My sed- and awk-fu is not up to speed, so how can I remove the empty configuration sections?

The first line of a configuration section matches ^\[.*\]$, and it is empty if the first line is followed by any number of lines consisting only of whitespace, then followed by another line matching ^\[.*\]$ or EOF.


Recall section headers as you see them, but don't print them until you see a setting line in that section. You could do it in sed by storing the section header in the hold space, but it's clearer in awk.

awk '
  /^ *\[/ {section=$0}    # recall latest section header
  /^ *[^[#]/ {            # setting line
    if (section != "") {print section; section="";}  # print section header if not yet done
' ~/.vlc/vlcrc >~/config/vlc/vlcrc
  • Cool; is it possible to do this in-place? – l0b0 Apr 8 '11 at 13:46
  • @l0b0: awk '…' vlcrc >vlcrc.new && mv vlcrc.new vlcrc. Or awk '…' vlcrc | sponge vlcrc. Or switch to Perl (perl -i -pe '…'). – Gilles 'SO- stop being evil' Apr 8 '11 at 19:15

As an alternative to an awk one-liner, you can store an awk script to a file. Here is a slightly more sophisticated version of the script:

#!/usr/bin/awk -f

# No section seen yet.
BEGIN { section="" }

# Match and save current section header.
/^\[.*\]$/ {

# Ignore blank and comment-only lines.
/^[[:space:]]*(#.*)?$/ {

# By default do this.
        # Print section header if not already printed.
        #if(length(section)) {
        if(section != "") {
                print section;

        # And print current line.

Just save it to a file like conf-filter.awk and mark it executable with chmod +x conf-filter.awk.


I realize default config files look messy, but down the road when you need to change some configuration value it is helpful to have all the headers and comments in place. From a long term administration standpoint is is usually advisable to keep these files in their original form. This also allows you to keep track of changes as you upgrade ... each new version of a program will change the default config, and if you have made as few changes as possible to yours, you will be able to easily compare using tools like diff or vim -d to see what has changed.

Having an awk script like people have you writing is nice to quickly glance at what values are being set, but it should be used to look at, not actually change and save your config files.


To keep only lines with text, I would personally use grep . instead.

  • I probably misinterpreted the question. I now see that you wish to remove from and including [header] up to the next [header] if there is nothing but comments and whitespace in between. – MattBianco Mar 31 '11 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.