The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to handle multiple rows in a file ksmbabu Shell Programming and Scripting 2 05-14-2008 09:44 PM
extract multiple sections of file rgentis Shell Programming and Scripting 6 03-24-2008 07:51 PM
flexible sed command needed to handle multiple input types SiftinDotCom Shell Programming and Scripting 2 03-19-2008 12:39 PM
extract multiple sections of a file rgentis UNIX for Advanced & Expert Users 1 03-18-2008 04:40 PM
How to handle the Multiple Rows in the Database hsekol Shell Programming and Scripting 2 03-05-2007 04:51 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-04-2008
potro's Avatar
Registered User
 

Join Date: Mar 2008
Posts: 36
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-04-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,279
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
It would be easier if you post a real sample of the input file and the desired output.

Regards
Reply With Quote
  #3 (permalink)  
Old 03-04-2008
potro's Avatar
Registered User
 

Join Date: Mar 2008
Posts: 36
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #4 (permalink)  
Old 03-05-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,279
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #5 (permalink)  
Old 03-05-2008
potro's Avatar
Registered User
 

Join Date: Mar 2008
Posts: 36
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 04:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102