LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-05-2012, 12:37 PM   #1
ISStaras
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Rep: Reputation: Disabled
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!

Last edited by ISStaras; 09-05-2012 at 05:17 PM.
 
Old 09-05-2012, 12:42 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
look into the grep command particularly the -f argument.
 
1 members found this post helpful.
Old 09-05-2012, 01:01 PM   #3
ISStaras
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
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?
 
Old 09-05-2012, 04:49 PM   #4
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,623

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
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)
 
Old 09-05-2012, 05:29 PM   #5
ISStaras
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by John VV View Post
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
 
Old 09-05-2012, 06:04 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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
 
1 members found this post helpful.
Old 09-05-2012, 06:54 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
1 members found this post helpful.
Old 09-06-2012, 12:12 PM   #8
ISStaras
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by chrism01 View Post
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!
 
Old 09-06-2012, 06:03 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Read a number x from a file and write a character in the xth column of other file homexylo Programming 7 08-05-2011 05:28 AM
[SOLVED] Bash, when I read sort write md5 file, the writen file have a space missing peter1234 Linux - General 3 09-21-2010 09:04 AM
Write into File from Kernel and read the same file from User space saurabhchokshi Programming 0 05-01-2009 02:26 PM
Can you write a script to display file name, Inode, and size of any file? JaxsunApex Linux - Newbie 3 01-30-2007 08:39 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration