a peek into my MIND

January 18, 2010

Reading a properties file in linux shell script.

Filed under: General — Bharat Kondeti @ 10:12 pm

It’s pretty easy to read a key=value style properties file using some sed and grep magic in a shell script

Example properties file:

DAS01-8090_workspace=/opt/home/dasadmin/workspace/trunk
DAS01-8090_profile=development-resources
DAS01-8090_serverLocation=/opt/apache/tomcat2
DAS01-8090_serverType=local

#DAS03-8090_workspace=/opt/home/dasadmin/workspace/branches/Production-2009-12-09
DAS03-8090_workspace=/opt/home/dasadmin/workspace/branches/Production-2010-01-14
DAS03-8090_profile=nuance-resources
DAS03-8090_serverLocation=/opt/apache/tomcat-iteration
DAS03-8090_serverType=remote
DAS03-8090_remoteMachine=dasadmin@pacdcdtadeva03

DAS03-8080_workspace=/opt/home/dasadmin/workspace/branches/Production-2010-01-14
DAS03-8080_profile=integration-resources
DAS03-8080_serverLocation=/opt/apache/tomcat
DAS03-8080_serverType=remote
DAS03-8080_remoteMachine=dasadmin@pacdcdtadeva03

Following script takes in a profile name and read’s the profile information accordingly

workspace=`sed '/^\#/d' environment.properties | grep "$1_workspace"  | tail -n 1 | sed 's/^.*=//'`
profile=`sed '/^\#/d' environment.properties | grep "$1_profile"  | tail -n 1 | sed 's/^.*=//'`
serverLocation=`sed '/^\#/d' environment.properties | grep "$1_serverLocation"  | tail -n 1 | sed 's/^.*=//'`
serverType=`sed '/^\#/d' environment.properties | grep "$1_serverType"  | tail -n 1 | sed 's/^.*=//'`
remoteMachine=`sed '/^\#/d' environment.properties | grep "$1_remoteMachine"  | tail -n 1 | sed 's/^.*=//'`

sed ‘/^\#/d’ — This will skip any line’s that start with ‘#’. ‘/d’ is similar to ‘-v’ option in grep.
grep “$1_workspace” — will grep for the respective profile line
tail -n 1 — if for some reason there are multiple grep matches pick only first one.
sed ‘s/^.*=//’ — Substitution magic is happening over here. I am replacing the string part till ‘=’ with empty string.

So if I pass in DAS03-8090 as profile name to the script, a value ‘/opt/home/dasadmin/workspace/branches/Production-2010-01-14’ will be set to the variable ‘workspace’

Advertisements

5 Comments »

  1. Awesome! Just what i needed. But SED is pretty complicated for newbies man! Thanks a lot!

    Comment by Shravan — July 6, 2010 @ 11:55 am

  2. Awesome! Beautiful solution!

    Comment by Anonymous — October 20, 2010 @ 8:28 pm

  3. What happens when the value contains a special character?
    ex.
    the line contains a $ character.

    DAS01-8090_profile=development-reso$ur#@ces

    and The value should be print as “development-reso$ur#@ces”

    Comment by bikash — February 6, 2011 @ 9:03 pm

  4. Hi,
    I have also faced some issues:
    – several values are chosen by grep from keys
    – value contains ‘=’ signs

    To solve this I’ve changes the proposed approach to:

    about_link=`sed ‘/^\#/d’ environment.properties | grep “about.link.$1[ =]” | tail -n 1 | sed ‘s/[^=]*=//’`

    Nevertheless, thanks man, this gave me a good starting point.

    Comment by yuradizhak — July 19, 2012 @ 5:36 am

  5. Nice post… really helpful

    Comment by anonymous — October 22, 2013 @ 2:24 pm


RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: