LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to write a .sh script to read configuration file and write into .txt file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-write-a-sh-script-to-read-configuration-file-and-write-into-txt-file-4175425799/)

ISStaras 09-05-2012 12:37 PM

How to write a .sh script to read configuration file and write into .txt file
 
Okay so i'm very new to this (my first shell script actually) and having a hard time knowing even where to start..

so my task is to create a shell script that will read a configuration file, find the specific info I need from the config file and write it into a .txt file.

Here's an example of the config file:

file name = config.properties

Code:

#Tue Oct 18 16:23:06 GMT-05:00 2011
#The env property comes from the -D option -- devel/systest/preprod/prod
environment=${devel:env}

#Define properties that vary by environment here
ldapConfig.url=${${environment}.ldapConfig.url}
ldapConfig.password=${${environment}.ldapConfig.password}
ldapConfig.userDN=${${environment}.ldapConfig.userDN}

ldapPool.timeBetweenEvictionRunsMillis=${${environment}.ldapPool.timeBetweenEvictionRunsMillis}
ldapPool.maxActiveConnections=${${environment}.ldapPool.maxActiveConnections}
ldapPool.initialPoolSize=${${environment}.ldapPool.initialPoolSize}
ldapPool.minEvictableIdleTimeMillis=${${environment}.ldapPool.minEvictableIdleTimeMillis}

hazelcast.cluster.members=${${environment}.hazelcast.cluster.members}

#Environment specific properties
devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapConfig.password=????
devel.ldapConfig.userDN=cn\=????,ou\=Users,ou\=Administration,o\=Cingular
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2
devel.ldapPool.maxActiveConnections=2
devel.ldapPool.minEvictableIdleTimeMillis=60000




What I need the script to do is go through the file and find info i need and paste into .txt file. This is an example of what I would like the .txt file to look like after i run the script on this config file:

Code:

environment=${devel:env}
devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2
devel.ldapPool.maxActiveConnections=2
devel.ldapPool.minEvictableIdleTimeMillis=60000

So basically it should read the file, find the specific variables (or specific configurations) I specify and write them into a .txt file so I can attach it to my report.


Help please!
Thank you all in advanced!

schneidz 09-05-2012 12:42 PM

look into the grep command particularly the -f argument.

ISStaras 09-05-2012 01:01 PM

Quote:

Originally Posted by schneidz (Post 4773433)
look into the grep command particularly the -f argument.


Okay, so read the man page for that and still confused. Can you show me an example that I can work off of? Will I need to have a separate grep -f command for every variable I would like copied?

John VV 09-05-2012 04:49 PM

Quote:

so my task is to create a shell script that will read a configuration file, find the specific info I need from the config file and write it into a .txt file.
unix unlike Microsoft dose NOT use file extensions
a text file is NOT defined by *.txt
a text file can be , well, anything
.csv ,.obj ,.sh ," no dot anything "

config files ARE text files
std input > std output
this sounds a bit like homework so an example of output
Code:

man grep > ~/man-grep-text
there is now a text file in your HOME folder
you can open that test file with any text editor ( kate, Gedit,Emacs,vi,nano)

ISStaras 09-05-2012 05:29 PM

Quote:

Originally Posted by John VV (Post 4773597)
this sounds a bit like homework

I am a software tester and want to do this to make things easier to attach to my test report after each cycle. Since our configurations for each load scenario will be changing constantly, I need to keep track of the configurations per each load I run.



But anyways, this seems like a simple task to do, I understand the concept and what needs to be done just not exactly sure how to write the script so it will work properly.

Is there a way that I can write a script and insert lets say a list of configurations that need to be copied from the file? Where I can manually change the specific content it should look for..


So i can lets say ask for these:

devel.ldapConfig.url
devel.ldapPool.timeBetweenEvictionRunsMillis
devel.ldapPool.initialPoolSize

and into my specified file it will copy this:

devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2

jschiwal 09-05-2012 06:04 PM

Quote:

#Tue Oct 18 16:23:06 GMT-05:00 2011
#The env property comes from the -D option -- devel/systest/preprod/prod
environment=${devel:env}
Is the "devel" from the environment line something that needs to be extracted, to determine the lines in the config to search for?

In other words, if the line was "environment=${prod:env}" would you look for lines:
Code:

prod.ldapConfig.url=ldap\://155.165.192.12\:389
prod.ldapConfig.password=????
prod.ldapConfig.userDN=cn\=????,ou\=Users,ou\=Administration,o\=Cingular
prod.ldapPool.timeBetweenEvictionRunsMillis=1234556
prod.ldapPool.initialPoolSize=2
prod.ldapPool.maxActiveConnections=2
prod.ldapPool.minEvictableIdleTimeMillis=60000

Please read the manpage for the grep command. The -f option will read patterns to match from a file instead of from arguments. You could instead use multiple patterns but typing them after the -e option.

Also consider using sed. Especially if the order is important and you can't rely on it in the source config file.
If "devel" is not a constant, extract the value first, and then use in another grep or sed command to filter out the lines you want:
Code:

ENVTYPE=$(sed '/environment=\${/s/.*{\(.*\):env}/\1/')
sed -n "/${ENVTYPE}.ldapConfig.url=/p;/${ENVTYPE}.ldapPool.timeBetweenEvictionRunsMillis=/p" config.properties >config-seg.txt

I only included a couple lines. Simply add patterns to match. I used doublequotes to allow the $ENVTYPE variable to be evaluated by bash _before_ the sed command is run. You can also create a file of sed commands and have one command per line. Then the second sed command will be like:
Code:

sed -n -f filterrules.sed config.properties >config-seg.txt

chrism01 09-05-2012 06:54 PM

This is a possible soln
Code:

# 1. given cfg file as specced

# 2. create match.dat

environment=
devel.ldapConfig.url=
devel.ldapPool.timeBetweenEvictionRunsMillis=
devel.ldapPool.initialPoolSize=
devel.ldapPool.maxActiveConnections=

# 3. do it

grep -f match.dat t.cfg

# 4. output

environment=${devel:env}
devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2
devel.ldapPool.maxActiveConnections=2
devel.ldapPool.minEvictableIdleTimeMillis=60000


ISStaras 09-06-2012 12:12 PM

Quote:

Originally Posted by chrism01 (Post 4773644)
This is a possible soln
Code:

# 1. given cfg file as specced

# 2. create match.dat

environment=
devel.ldapConfig.url=
devel.ldapPool.timeBetweenEvictionRunsMillis=
devel.ldapPool.initialPoolSize=
devel.ldapPool.maxActiveConnections=

# 3. do it

grep -f match.dat t.cfg

# 4. output

environment=${devel:env}
devel.ldapConfig.url=ldap\://155.165.192.12\:389
devel.ldapPool.timeBetweenEvictionRunsMillis=1234556
devel.ldapPool.initialPoolSize=2
devel.ldapPool.maxActiveConnections=2
devel.ldapPool.minEvictableIdleTimeMillis=60000






Exactly what I was looking for! Thank you all for you help. chrism01 you rock! :)

chrism01 09-06-2012 06:03 PM

No worries :)

As you're new to shell scripting, I highly recommend these links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

sed & awk tools (& other stuff) http://www.grymoire.com/Unix/


All times are GMT -5. The time now is 11:45 AM.