extracting values from configuration file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting values from configuration file
# 1  
Old 02-28-2011
extracting values from configuration file

Dear All,

i am new to shell scripting, I am working on embedded system based on linux.I am supposed to the read the configuration file and edit another file.
presently I would like to read from the configuration file.It would be having values file one below.

There is chance of entering duplicate variable in configuration file however I need to extaract only the last entered value.

example file.conf
Code:
ChangeVersion=0
IPAddress=192.168.1.195
GATEIPAddress=192.168.1.1
WebServerIP=192.168.1.106
IPAddress=192.168.1.201
IPAddress=192.168.1.223

I need to extarct the change version,GATEIPAddress and IPAddress which comes as the last entry. How we can achive this.

Thanks in Advance,
Ratheendran

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 02-28-2011 at 08:48 AM.. Reason: code tags
# 2  
Old 02-28-2011
You could source the file. the duplicates will always have the last entry since it overwrites the previous entry.

Code:
. file.conf
or
source file.conf

# 3  
Old 02-28-2011
See if this would work for you:
Code:
sed -n -e '/ChangeVersion/p' -e '/GATEIP/p' -e '$p' inp_file

# 4  
Old 02-28-2011
Code:
# cat tst
ChangeVersion=0
IPAddress=192.168.1.195
GATEIPAddress=192.168.1.1
WebServerIP=192.168.1.106
IPAddress=192.168.1.201
IPAddress=192.168.1.223

Code:
# nawk '/^IPAddress/{x=$0}/GATE/||/Version/{print}END{print x}' tst
ChangeVersion=0
GATEIPAddress=192.168.1.1
IPAddress=192.168.1.223

Code:
# nawk -F= '/ChangeVersion/||/IPAddress/{A[$1]=$2}END{for(i in A) print i"="A[i]}' tst
GATEIPAddress=192.168.1.1
ChangeVersion=0
IPAddress=192.168.1.223

if it is sure that IPAddress is in last line then
Code:
# nawk '{x=$0}/GATE/||/Version/END{print x}' tst
ChangeVersion=0
GATEIPAddress=192.168.1.1
IPAddress=192.168.1.223


Last edited by ctsgnb; 02-28-2011 at 12:25 PM..
# 5  
Old 03-01-2011
Thanks every one for your response.

I guess I need to make the requirements more clear.

I would like to get the values from config file and store them to a variable,since the chances of entering the duplicate fields are obvious.



Code:
 
#cat tst

Code:
WebServerIP=192.168.1.106       
ChangeVersion=0                      
IPAddress=192.168.1.195              
GATEIPAddress=192.168.1.1            
GATEIPAddress=192.168.1.102            
WebServerIP=192.168.1.106            
IPAddress=192.168.1.201              
IPAddress=192.168.1.223              
HiSpeedNet=1                         
HiSpeedNet=2 



so I need to retrieve last entered field and store it in a variable for subsequent manipulation.
e.g HiSpeedNet=2, IPAddress=192.168.1.223, GATEIPAddress=192.168.1.102 ..etc.
# 6  
Old 03-01-2011
Could this help you ?
Code:
awk -F"=" '{b[$1]=$0;next} END{for(i in b) {print b[i]}}' testfile

# 7  
Old 03-01-2011
Quote:
Originally Posted by Ratheendran

so I need to retrieve last entered field and store it in a variable for subsequent manipulation.
e.g HiSpeedNet=2, IPAddress=192.168.1.223, GATEIPAddress=192.168.1.102 ..etc.
The first response is the answer you're looking for. See post #2 by anchal_khare.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting values after the maximum value in a txt file

Hello, I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working: 0.3092381 0.3262799 0.3425480 0.3578379 0.3719490 0.3846908 0.3958855 0.4053738 0.4130160 0.4186991 0.4223357 0.4238688 ... (14 Replies)
Discussion started by: jeo_fb
14 Replies

2. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

3. Shell Programming and Scripting

Extracting unique values of a column from a feed file

Hi Folks, I have the below feed file named abc1.txt in which you can see there is a title and below is the respective values in the rows and it is completely pipe delimited file ,. ... (4 Replies)
Discussion started by: punpun66
4 Replies

4. UNIX for Dummies Questions & Answers

Fixed length file extracting values in columns

How do I extract values in a few columns in a row of a fixed length file? If there are 8 columns and I need to extract values of 2nd,4th and 6 th columns, how do i do that? I used cut command, this I used only for one column. How do I do it more than one column? The below command will give... (1 Reply)
Discussion started by: princetd001
1 Replies

5. Shell Programming and Scripting

Script to increase Timeout values in Configuration File

Hi Guys I am using one configuration file for reading some time out values.The format of the file is A.Type = Number A.Val = 2000 B.Type = Number B.Val = 4000 Now my requirement is I need to write a shell script in Solaris where i need to increase these timeout values by 10 times of... (3 Replies)
Discussion started by: mr_deb
3 Replies

6. UNIX for Dummies Questions & Answers

extracting values from file using perl

I would like to read value after $ symbol from a file. For eg a file will be having the following lines. 5,$0,-32768p,32767p,$400014f8,1,, 5,$0,0,0,$400008ce,1,, 5,$0,0,0,$400008d0,1,, i would like to read 400014f8 then 400014f8 and similar all the value after $ symbol.Can any one help... (1 Reply)
Discussion started by: jagadeeshrr
1 Replies

7. UNIX for Dummies Questions & Answers

Issue with use of Configuration file instead of hardcoded values inside the script

Hi, My code works perfectly fine. But, $my $min_to_add = 1 * 1 * 60; and my $hr_to_sub = 1 * 1 * 86400; i may need to change the values in future. so am keeping them in a separate configuration file like MIN = 1 * 1 * 60 HR = 24 * 60 * 60 in the script, i use a package use et_config... (3 Replies)
Discussion started by: irudayaraj
3 Replies

8. UNIX for Dummies Questions & Answers

Extracting values from an XML file

Hello People, I have an xml file from which I need to extract the values of the parameters using UNIX shell commands. Ex : Input is like : <Name>Roger</Name> or <Address>MI</Address> I need the output as just : Roger or MI with the tags removed. Please help. (1 Reply)
Discussion started by: sushant172
1 Replies

9. Programming

Extracting Field values for XML file

i have an input file of XML type with data like <nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-charging:process> i want to extract data such that i get the output... (3 Replies)
Discussion started by: junaid.nehvi
3 Replies

10. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies
Login or Register to Ask a Question