Solved

Parse config file in bash?

Posted on 2011-09-07
4
628 Views
Last Modified: 2012-06-21
I need my bash script to parse a configuration file. The configuration file needs to hold some specific information:

- a mapping of keys and their values
- a large block of text
- some lines might be comments
- some lines might be whitespace

I imagine one possible format for the config file (although it doesn't have to be done this way) is something like this:

[mapping]

; a comment here
foo=bar
asdf=qwerty

; more comments here
aaa=bbb
banana=orange

[text]
alsidjf alsif jasli fjasli fajslif ja
 ija liaj ilas jalsi jasl
 ijas lifjas lifjas lisja lij

Open in new window



How can I parse something like this in Bash? Do I have to write the parsing myself, or does Bash have some built in config-file-parsing abilities?
0
Comment
Question by:Frosty555
  • 2
4 Comments
 
LVL 20

Expert Comment

by:edster9999
ID: 36498573
How about you keep it simple and make the config file a script.

so the main script includes it with something like

#!/bin/bash
MY_DIR=`dirname $0`
source $MY_DIR/config.sh

echo "foo is set to $foo and asdf is set to $asdf"

echo -e "The text block is $text"




and the config file is :

# [mapping]
# a comment here

foo="bar"
asdf="qwerty"

# more comments here
aaa="bbb"
banana="orange"

# [text]
text="alsidjf alsif jasli fjasli fajslif ja\n\
 ija liaj ilas jalsi jasl\n\
 ijas lifjas lifjas lisja lij\n"

Open in new window

0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36499606
Just need the key and value pairs?

awk '/\=/ {print}'  /path-to-config-file
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 0 total points
ID: 36543415
Hi guys,

I didn't want to make it a bash script because it needs to be edited and created by people who do not necessarily know bash. Wesly, your awk line leaves the comments in and doesn't trim any spaces.

I ended up parsing the file through a much larger bash script that was essentially a state machine that looped through it line by line and split out the parts that it needed.

My requirements for the mapping file changed a little over the course of this project. I now needed to have a [mapping], [postupgrade] and [preupgrade] sections.

Here's how I ended up doing it

    # parse the mapping file
    mode=none
    rm -f /tmp/postupgrade.sql /tmp/preupgrade.sql /tmp/mapping.txt /tmp/upgrade.sql

    cat "$mapfile" | while read line; do

        # trim whitespace off the beginning and end of the line
        line=`echo "$line" | awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }'`

        # change mode
        if [ "$line" = "[mapping]" ]; then
            mode=mapping
            #echo "Entered mapping mode"
            continue
        fi
        if [ "$line" = "[postupgrade]" ]; then
            mode=postupgrade
            #echo "Entered postupgrade mode"
            continue
        fi
        if [ "$line" = "[preupgrade]" ]; then
            mode=preupgrade
            #echo "Entered preupgrade mode"
            continue
        fi

        case "$mode" in
            mapping)
                if [ "${line:0:1}" == ";" ] || [ -z "$line" ]; then
                    :
                    #echo "discarded line: $line"
                else
                    echo "$line" >>/tmp/mapping.txt
                    #echo "mapping line: $line"
                fi
                ;;

            postupgrade)
                echo "$line" >>/tmp/postupgrade.sql
                #echo "postupgrade line: $line"
                ;;

            preupgrade)
                echo "$line" >>/tmp/preupgrade.sql
                #echo "preupgrade line: $line"
                ;;

            *)
                #echo "discarded line: $line"
        esac

Open in new window


Then I followed a different expert's suggestion in another question for how exactly to parse the mapping.txt file further using awk.
0
 
LVL 31

Author Closing Comment

by:Frosty555
ID: 36565438
Ended up coding it myself.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question