I have a config.ini file which looks like this (updated):
[group1]
base=100
hwadd=0x20
doorstatus=100
lock=101
bookingnr=010100
kode=1111
inuse=0
[group2]
base=100
hwadd=0x20
doorstatus=100
lock=102
bookingnr= 010101
kode=1111
inuse=0
[group3]
base=100
hwadd=0x20
doorstatus=100
lock=103
bookingnr=010103
kode=1111
inuse=0
[group4]
base=100
hwadd=0x20
doorstatus=100
lock=105
bookingnr=010105
kode=1111
inuse=0
[group5]
base=100
hwadd=0x20
doorstatus=100
lock=106
bookingnr=010106
kode=1111
inuse=0
[group6]
base=100
hwadd=0x20
doorstatus=100
lock=107
bookingnr=010107
kode=1111
inuse=0
[group7]
base=100
hwadd=0x21
doorstatus=100
lock=101
bookingnr=010108
kode=1111
inuse=0
[group8]
base=100
hwadd=0x21
doorstatus=100
lock=102
bookingnr=010109
kode=1111
inuse=0
...
I need to get the room number where 3 given values (base, hwadd, lock) match the script parameters. So lets say the bash script was called like this:
script.sh 100 0x20 105
the script would return 4 (since all 3 parameters match group4)
Finding a single value is a piece of cake
source config.ini
and every value is available under $valX but this is not parsing the section names so it is useless in this case..
Whenever your input data has name-value pairs it's best to first create an array (f[]
below) that maps those names to their values and then at the end of every record you can simply compare the values by their names in whatever combination you like and print whatever values you like in whatever order you like just referencing them by their names.
$ cat tst.awk
BEGIN { FS=" *= *"; OFS="=" }
NF {
if ( /]/ ) {
gsub(/[^0-9]/,"")
f["group"] = $0
}
else {
f[$1] = $2
}
next
}
{ prt() }
END { prt() }
function prt() {
if ( (f["base"] == base) && (f["hwadd"] == hwadd) && (f["lock"] == lock) ) {
print f["group"]
}
delete f
}
$ awk -v base=100 -v hwadd=0x20 -v lock=105 -f tst.awk file
4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With