Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a config file in bash

Tags:

grep

bash

sed

awk

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.

like image 500
Big McLargeHuge Avatar asked Dec 09 '22 15:12

Big McLargeHuge


2 Answers

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:

  • Check the first field against our required strings $1=="banned-phrase"

  • If the first field matched then loop over all the other fields for(i=2;i<=NF;i++) and print each field print $i and redirect the output to the file >"bannedphraselist".

like image 184
Chris Seymour Avatar answered Dec 11 '22 12:12

Chris Seymour


You could do

sed -n 's/^banned-phrase *//p' dansguardian-config > bannedphraselist
sed -n 's/^banned-site *//p' dansguardian-config > bannedsitelist

Although that means reading the file twice. I doubt that the possible performance loss matters though.

like image 38
Lev Levitsky Avatar answered Dec 11 '22 10:12

Lev Levitsky