Parse Multi-Section Configuration File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse Multi-Section Configuration File
# 1  
Old 02-27-2010
Question Parse Multi-Section Configuration File

Hello all,

Sample configuration file:

Code:
[NETWORK-CAMERA.C1]
username = root
password = admin
IpAddress = 192.168.2.90
HttpCommand = /getfile?hello.jpg
[DATA-CENTER.S1]
Username = root2
Password = admin2
Passive = no
Host = 192.168.1.100
Path = /uploads
[DATA-CENTER.S2]
Username = root3
Password = adming
Passive = no
Host = 192.168.1.101
Path = /uploads

Can someone help me parse this configuration file by calling a section e.g DATA-CENTER.S2 and have it output key-value to an array?

Last edited by Scott; 02-27-2010 at 09:53 AM.. Reason: Please use code tags
# 2  
Old 02-27-2010
Code:
$ X=($(awk '/\[/ { p = 0 } /\[DATA-CENTER.S2]/ { p = 1; next } {sub(/.* *= */, "")} p' file1))

$ echo ${X[@]}
root3 adming no 192.168.1.101 /uploads

$ echo ${X[0]}
root3

$ echo ${X[1]}
adming


$ Y=($(sed -n "/\[DATA-CENTER.S2]/,/\[/ {/\[/d;s/.* *= *//;p;}" file1))

$ echo ${Y[@]}
root3 adming no 192.168.1.101 /uploads

$ echo ${Y[0]}
root3

$ echo ${Y[1]}
adming

# 3  
Old 02-27-2010
slick, thank you so much!

---------- Post updated at 01:33 PM ---------- Previous update was at 11:20 AM ----------

ok this is odd...when I try it within the console it works fine but when I try it within a schell (.sh) script I get syntax error.

my_array=( one two three )

syntax error Ļ(Ļ unexpected

even with a simple array test I get the same error, any ideas...
# 4  
Old 02-27-2010
Which shell are you using - and how are you running the script?

i.e.
Code:
./my_script

or
Code:
sh ./my_script

# 5  
Old 02-27-2010
configured it out...

I had #!/bin/sh and once I changed it to #!/bin/bash it worked.
# 6  
Old 02-27-2010
For old shell try:
Code:
#!/bin/sh
set -- $(awk '/\[DATA-CENTER.S1]/{f=1;next}/\[/{f=0}f{print $NF}' file)
echo $@
echo $1

# 7  
Old 02-27-2010
Havenīt touched shell in years, but itīs fun to get back into it (missed scratching my head), LOL!

Thanks again everyone...

---------- Post updated at 05:49 PM ---------- Previous update was at 01:59 PM ----------

I should have provided additional value scenarios but the above is not parsing the following key-values:

Code:
[STATION]
Region = NL
Organization = NLDOT
StationName = "Chateau Pond"
StationClientId = NLPCP
StationNetworkId = NLDOT_NLPCP
[NETWORK-CAMERA.C1]
Username = root
Password = admin
Host = 192.168.2.90
HttpCommand = /axis-cgi/jpg/image.cgi?resolution=320x240
Retries = 3
[DATA-CENTER.S1]
Username = root2
Password = admin2
Passive = no
Host = 192.168.1.100
Path = /uploads
[DATA-CENTER.S2]
Username = root3
Password = admin3
Passive = no
Host = 192.168.1.101
Path = /upload

any assistance would be greatly appreciated.

Last edited by LAVco; 02-27-2010 at 04:56 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse section of csv into array

In the awk below I am trying to parse the Sample Name below the section. The values that are extracted are read into array s(each value in a row seperated by a space) which will be used later in a bash script. The awk does execute but no values are printed. I am also not sure how to print in a row... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Xml multi parse question

awk -F "" '/<Id>|<id>|<Source>|<source>|<Accession>|<accession>|<TestName>|<testname>/ {print $2, $3}' OFS='\t' Test.xml The above code works great, but lets say I wanted the <Analyte> name (<Name>STAT3). The word <name> is unique so there will be multiple records pulled. Is there a way to... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

4. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

5. Shell Programming and Scripting

Delete a section of a file if...

i have a file as below that has n section : 2006 0101 1236 49.3 L 37.902 48.482 0.0 Teh 5 0.2 2.7LTeh 1 GAP=238 E Iranian Seismological Center, Institute of Geophysics, University of Tehran 6 ... (5 Replies)
Discussion started by: oreka18
5 Replies

6. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

7. Shell Programming and Scripting

Parse configuration file & add line in particular section

Greetings, I recently built a replicated DRBD, Heartbeat, & iSCSI Target Initiator storage server on Ubuntu 10.04 to offer shared storage to server Vmware ESX and Microsoft Clusters. Everything works flawlessly, however I wanted to make a script to create, remove, grow volumes to offer ESX... (6 Replies)
Discussion started by: Aeudian
6 Replies

8. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

9. Shell Programming and Scripting

Search and extract by section from configuration

Hi, I understand either AWK or SED can do this, but I not sure how to extract the following configuration in section. Meaning when I need to find code with " ip helper-address 192.168.11.2" , it would start from "interface Serial0/0" and "interface FastEthernet0/1". Only displaying both section... (2 Replies)
Discussion started by: haphazard
2 Replies

10. UNIX for Dummies Questions & Answers

multi-file multi-edit

Good day! I am trying to learn how to use the "sed" editor, to perform multiple edits on multiple files in multiple directories. I have one script that tries to call up each file and process it according to the edits listed in a second script. I am using a small input text to test these, at... (12 Replies)
Discussion started by: kielitaide
12 Replies
Login or Register to Ask a Question