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

It would be easier if you post a real sample of the input file and the desired output.

Regards

[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

Try this:

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

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

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:

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

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

Hi

Somebody managed to get me out of this problem with a function:

get_param(){
(($#==3))||{ printf "Usage: %s [fname] [section] [param]\n" $0;return 1;}
awk '$1=="["s"]"{f++}f&&$1~p{print $2;exit}' s="$2" p="$3" FS=" *= *" "$1"
}

Thanks for your time,
Bianca