bashsedawkgrep

Parsing a config file in bash


Here's my config file (dansguardian-config):

banned-phrase duck

banned-site allaboutbirds.org

I want to write a bash script that will read this config file and create some other files for me. Here's what I have so far, it's mostly pseudo-code:

while read line
do
    # if line starts with "banned-phrase"
        # add rest of line to file bannedphraselist
    # fi

    # if line starts with "banned-site"
        # add rest of line to file bannedsitelist
    # fi
done < dansguardian-config

I'm not sure if I need to use grep, sed, awk, or what.

Hope that makes sense. I just really hate DansGuardian lists.


Solution

  • With awk:

    $ cat config
    banned-phrase duck frog bird
    banned-phrase horse
    banned-site allaboutbirds.org duckduckgoose.net
    banned-site froggingbirds.gov
    
    $ awk '$1=="banned-phrase"{for(i=2;i<=NF;i++)print $i >"bannedphraselist"}
           $1=="banned-site"{for(i=2;i<=NF;i++)print $i >"bannedsitelist"}' config
    
    $ cat bannedphraselist 
    duck
    frog
    bird
    horse
    
    $ cat bannedsitelist 
    allaboutbirds.org
    duckduckgoose.net
    froggingbirds.gov
    

    Explanation:

    In awk by default each line is separated into fields by whitespace and each field is handled by $i where i is the ith field i.e. the first field on each line is $1, the second field on each line is $2 upto $NF where NF is the variable that contains the number of fields on the given line.

    So the script is simple: