Handle Configuration File with same name of Parameter in multiple Sections


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handle Configuration File with same name of Parameter in multiple Sections
# 1  
Old 03-04-2008
Handle Configuration File with same name of Parameter in multiple Sections

Hi

I have a config file with multiple section and a parameter with the same name in each section. I need to read each parameter for distinct section.

[Section1]
Parameter = 1
....
[Section2]
Parameter = 2
....
[Section3]
Parameter = 4
....

Tried this:
grep -m1 '^[^#][:space:]*ProcessorsNumber' ServiceBrokerFramework.cfg | awk -F" = " '{print $2}'

but the output is only the first occurence found in the config file.

Thanks,
Bianca
# 2  
Old 03-04-2008
It would be easier if you post a real sample of the input file and the desired output.

Regards
# 3  
Old 03-05-2008
Config File example

[Billing]
LocalIPAddress = 192.168.1.116
ProcessorsNumber = 1
[Plugins]
LocalIPAddress = 192.168.2.116
ProcessorsNumber = 2
[Statistics]
LocalIPAddress = 192.168.3.116
ProcessorsNumber = 1

I manage to read like this:
BILLPROCESSORSNUMBER=`grep -m1 '^[^#][:space:]*ProcessorsNumber' $OLD_CFG | awk -F" = " '{print $2}' |tail -1`

PLUGINPROCESSORSNUMBER=`grep -m2 '^[^#][:space:]*ProcessorsNumber' $OLD_CFG | awk -F" = " '{print $2}' |tail -1`

STATPROCESSORSNUMBER=`grep -m3 '^[^#][:space:]*ProcessorsNumber' $OLD_CFG | awk -F" = " '{print $2}' |tail -1`

But I'm worried if the sections in the config file will switch and the order not preserved ... then the grep -mX and tail will not give me the proper value for the section/parameter I want.

I want to read this values and later to write them back in the new config file with:
sed -e "/\[Billing\]/,/^[^#][:space:]*ProcessorsNumber =/ s-\(^[^#][:space:]*ProcessorsNumber = \)\([^#]*\)-\1$BILLPROCESSORSNUMBER-1" \
This sed works.

So it is alll about how to read correct from the file.

Thanks
Bianca
# 4  
Old 03-05-2008
Try this:

Code:
BILLPROCESSORSNUMBER=`awk -v file=$OLD_CFG '/Billing/ {getline;getline;print $3}' file`
PLUGINPROCESSORSNUMBER=`awk -v file=$OLD_CFG '/Plugins/ {getline;getline;print $3}' file`
STATPROCESSORSNUMBER=`awk  -v file=$OLD_CFG '/Statistics/ {getline;getline;print $3}' file`

Regards
# 5  
Old 03-05-2008
This doesn't work because the config file has many lines (text and other parameters) in between and the parameters position is not fixed.


[Billing]
#texttex
LocalIPAddress = 192.168.1.116
Another Parameter = xxx
ProcessorsNumber = 1
[Plugins]
ProcessorsNumber = 2
#text text
LocalIPAddress = 192.168.2.116
[Statistics]
#text text
#text text
LocalIPAddress = 192.168.3.116
#text text
ProcessorsNumber = 1
# 6  
Old 03-05-2008
That's why I asked for a real sample of your input file.
Most of us are hard-working professionals, we like to help but we have more to do than answering wrong formulated questions.

Ok, try this:

Code:
BILLPROCESSORSNUMBER=`awk -v file=$OLD_CFG awk '/Billing/{f=1;while($1 != "ProcessorsNumber"){getline}}f{print $3;exit}' file`
PLUGINPROCESSORSNUMBER=`awk -v file=$OLD_CFG awk '/Plugins/{f=1;while($1 != "ProcessorsNumber"){getline}}f{print $3;exit}' file`
STATPROCESSORSNUMBER=`awk -v file=$OLD_CFG awk '/Statistics/{f=1;while($1 != "ProcessorsNumber"){getline}}f{print $3;exit}' file`


Regards
# 7  
Old 03-05-2008
My mistake for not explaining the entire pb:
The file.cfg
[GetAuthRequest]
Plugin1 = Billing
Plugin2 = Preview
Plugin4 = AnticipateResponse
Plugin5 = Plugins

[BillingInfoReportRequest]
Plugin1 = Plugins
Plugin2 = Statistics
Plugin5 = Bookmarks
Plugin6 = Preview
Plugin7 = Billing
#text text
[Billing]
#texttex
LocalIPAddress = 192.168.1.116
Another Parameter = xxx
ProcessorsNumber = 1
[Plugins]
ProcessorsNumber = 2
#text text
LocalIPAddress = 192.168.2.116
[Statistics]
#text text
#text text
LocalIPAddress = 192.168.3.116
#text text
ProcessorsNumber = 1

So the section NAME can be present for more than once in the file.cfg but the section [NAME] only once.

This is the result after running you command:

[bianca@skynet bianca]# a=`awk -v file=file.cfg awk '/Statistics/{f=1;while($1 != "ProcessorsNumber"){getline}}f{print $3;exit}' file`
awk: cmd. line:2: fatal: cannot open file `/Statistics/{f=1;while($1 != "ProcessorsNumber"){getline}}f{print $3;exit}' for reading (No such file or directory)

Thanks,
Bianca
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace multiple file by passing parameter value

Hello All, I want to change date part in file name to yesterday date in the file name. example file name file-12122017-06-30-41.dat want file-12112017-06-30-41.dat I am doing like below. Below it is not changing the filename. Actually it is not parsing the $today and $yesterday value in... (1 Reply)
Discussion started by: looney
1 Replies

2. Shell Programming and Scripting

CSV File with Multiple Search Parameter

Dear Team Members, I have a unique problem. Below is the dataset which I have. I am writing a script which will read through the file and pull the invoice no. (Field 2 of C1 row). "C1",990001,"L1","HERO","MOTORCYCLE","ASIA-PACIFIC","BEIJING" "C2","CLUTCH","HYUNDAI",03032017... (13 Replies)
Discussion started by: chetanojha
13 Replies

3. UNIX for Advanced & Expert Users

How does extundelete handle multiple versions of the same inode?

Hi, I noticed a weird behavior in extundelete way to choose the filename to which it will restore a given inode. Here is an example : root@rescue:~# for after in '' 0 740 741 $(date -d 'now - 1 year' +%s); do rm -rf RECOVERED_FILES/; echo -e "$(date -d@$after 2> /dev/null || echo No... (4 Replies)
Discussion started by: chebarbudo
4 Replies

4. Shell Programming and Scripting

Multiple file and single parameter file

Hi Team, In our project we have written below 2 scripts like Script1: Shell script start & END Begin Audit process - uses teradata bteq END Audit Process Script 2: Environemtal variable file different Now Client ask to change this requirement and need below files: Script1:... (1 Reply)
Discussion started by: tusharzaware1
1 Replies

5. UNIX for Dummies Questions & Answers

how to handle multiple apps on host?

So, I'm going to install Oracle DB within my Suse host... Also, I would like to run Virtual Box and Tomcat.. ok, tomcat is going to be runnable app that will start at boot.. but, how to handle Oracle and virtual box? I would like to have an Oracle under it's own user, and to be able to use... (0 Replies)
Discussion started by: bongo
0 Replies

6. Shell Programming and Scripting

How to edit file sections that cross multiple lines?

Hello, I'm wondering where I could go to learn how to edit file sections that cross multiple lines. I'm wanting to write scripts that will add Gnome menu entries for all users on a system for scripts I write, etc. I can search an replace simple examples with sed, but this seems more complex. ... (8 Replies)
Discussion started by: Narnie
8 Replies

7. Shell Programming and Scripting

How to handle multiple rows in a file

I have a FILE a.txt which has the following data 113901.94,113901.94,56950.97,56950.97,NOT MATCHING,NOT MATCHING 10693.04,10693.04,5346.52,5346.52,NOT MATCHING,NOT MATCHING 1901.94,1901.94,550.97,550.97,NOT MATCHING,NOT MATCHING 103.04,103.04,53.52,53.52,NOT MATCHING,NOT MATCHING #### This... (2 Replies)
Discussion started by: ksmbabu
2 Replies

8. Shell Programming and Scripting

extract multiple sections of file

I have a file that I need to parse multiple sections from the file. The file contains multiple lines that start with ST (Abunch of data) Then the file contains multiple lines that start with SE (Abunch of data) SE*30*0001 ST*810*0002 I need all of the lines between and including these.... (6 Replies)
Discussion started by: rgentis
6 Replies

9. UNIX for Advanced & Expert Users

extract multiple sections of a file

I have a file that I need to parse multiple sections from the file. The file contains multiple lines that start with ST (Abunch of data) Then the file contains multiple lines that start with SE (Abunch of data) SE*30*0001 ... (1 Reply)
Discussion started by: rgentis
1 Replies

10. Shell Programming and Scripting

How to handle the Multiple Rows in the Database

Hi All, I have problem with database validations, actually my requirement is, my code will generate some seqno;s, which i have to check in database, whether the generated seqno;s are present in database or not, if the generated seqno;s are present in the database means, i need to generate... (2 Replies)
Discussion started by: hsekol
2 Replies
Login or Register to Ask a Question