Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers 4

up vote 1 down vote accepted

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
    print
  }
' ~/.vlc/vlcrc >~/config/vlc/vlcrc
share|improve this answer
    
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 Apr 8 '11 at 19:15
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
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
add comment

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.
/^\[.*\]$/ {
        section=$0;
        next;
}

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

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

        # And print current line.
        print;
}

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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