Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
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: ...
  1. #1
    Just 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

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    which part of the example configuratoin file do you actually want to grab?

  3. #3
    Just 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)

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    only tested on sample input and assuming always ProcessorNumber and localIP at every block
    Code:
    awk 'BEGIN{FS="="}
    /^\[/ {
       line=$0
       getline 
       IP=$2
       getline   
       ProcessorNumber=$2
       print line,IP,ProcessorNumber
       }
    ' "file"
    output:
    Code:
    # ./test.sh
    [Billing]  192.168.1.116  1
    [Plugins]  192.168.2.116  2
    [Statistics]  192.168.3.116  1

  5. #5
    Just 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

  6. #6
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    % 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

  7. #7
    Just 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

  8. #8
    Just 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

  9. #9
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    You can use a function:

    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"
    }
    For example:

    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

  10. #10
    Just 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

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...