Results 1 to 10 of 26
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:
...
- 03-05-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 30
Grep+AWK Handle Configuration File with same name of Parameter in multiple Sections
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.
Thanks,
Bianca
- 03-05-2008 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
which part of the example configuratoin file do you actually want to grab?
- 03-05-2008 #3Just Joined!
- Join Date
- Mar 2008
- Posts
- 30
I want to take for each section the ProcessorsNumber parameter into separate variables:
For example:
1. for [Billing] section I will have the BILLPROCESSORSNUMBER variable with the ProcessorsNumber value from this section.
2. for [Plugins] section I will have the PLUGINPROCESSORSNUMBER variable with the ProcessorsNumber value from this section.
and further I will do the same with the other parameters in the config file (such as LocalIPAddress)
- 03-05-2008 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
only tested on sample input and assuming always ProcessorNumber and localIP at every block
output:Code:awk 'BEGIN{FS="="} /^\[/ { line=$0 getline IP=$2 getline ProcessorNumber=$2 print line,IP,ProcessorNumber } ' "file"
Code:# ./test.sh [Billing] 192.168.1.116 1 [Plugins] 192.168.2.116 2 [Statistics] 192.168.3.116 1
- 03-05-2008 #5Just Joined!
- Join Date
- Mar 2008
- Posts
- 30
This doesn't work in my case 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
Another Parameter = yyy
Another Parameter = zzz
LocalIPAddress = 192.168.2.116
[Statistics]
#text text
#text text
LocalIPAddress = 192.168.3.116
#text text
ProcessorsNumber = 1
- 03-05-2008 #6Code:
% awk '/^\[/{s=$2}$0~p{printf "%s_%s=%s\n",s,p,$2}' p="Processors" FS=" *= *|[][]" file Billing_Processors=1 Plugins_Processors=2 Statistics_Processors=1 % awk '/^\[/{s=$2}$0~p{printf "%s_%s=%s\n",s,p,$2}' p="IP" FS=" *= *|[][]" file Billing_IP=192.168.1.116 Plugins_IP=192.168.2.116 Statistics_IP=192.168.3.116
- 03-05-2008 #7Just Joined!
- Join Date
- Mar 2008
- Posts
- 30
Hi radoulov
Your awk gave me this:
awk '/^\[/{s=$2}$0~p{printf "%s_%s=%s\n",s,p,$2}' p="ProcessorsNumber" FS=" *= *|[][]" config.file
output:
Billing_ProcessorsNumber=1
Plugins_ProcessorsNumber=2
Statistics_ProcessorsNumber=4
It took the correct values from my config.file
But how can I give the values to variable for each section ?
BILL_PROCESSORS=awk '/^\[/{s=$2}$0~p{printf "%s_%s=%s\n",s,p,$2}' p="ProcessorsNumber" FS=" *= *|[][]" config.file
Where in this command should I mention the Billing section for which I need the parameter value?
Thanks a lot,
Bianca
- 03-05-2008 #8Just Joined!
- Join Date
- Mar 2008
- Posts
- 30
BILL_PROCESSORS=`awk '/^\[/{s=$2}$0~p{printf "%s_%s=%s\n",s,p,$2}' p="ProcessorsNumber" FS=" *= *|[][]" /etc/ServiceBrokerFramework.cfg |grep Billing |awk -F"=" '{print $2}'`
This does the job.
Do you have a more elegant solution?
Thanks,
Bianca
- 03-05-2008 #9
You can use a function:
For example:Code:get_param(){ (($#==3))||{ printf "Usage: %s [fname] [section] [param]\n" $0;return 1;} awk '$0~s{f++}f&&$1~p{print $2;exit}' s="$2" p="$3" FS=" *= *" "$1" }
Code:% cat config [Billing] #texttex LocalIPAddress = 192.168.1.116 Another Parameter = xxx ProcessorsNumber = 1 [Plugins] ProcessorsNumber = 2 #text text Another Parameter = yyy Another Parameter = zzz LocalIPAddress = 192.168.2.116 [Statistics] #text text #text text LocalIPAddress = 192.168.3.116 #text text ProcessorsNumber = 1 % get_param config Plugins IP 192.168.2.116 % bill_ip="$(get_param config Billing IP)" % echo "$bill_ip" 192.168.1.116
- 03-05-2008 #10Just Joined!
- Join Date
- Mar 2008
- Posts
- 30
Works only for the first section
[root@skynet bianca]# cat xbs.sh
#!/bin/bash
get_param(){
(($#==3))||{ printf "Usage: %s [fname] [section] [param]\n" $0;return 1;}
awk '$0~s{f++}f&&$1~p{print $2;exit}' s="$2" p="$3" FS=" *= *" "$1"
}
plugin_processors="$(get_param config Plugins ProcessorsNumber)"
echo $plugin_processors
Return value: 1 instead of 2.
Bianca


Reply With Quote