[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Parse config file in bash?

Posted on 2011-09-07
4
Medium Priority
?
695 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

649 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