Dear All,
i am new to shell scripting, I am working on embedded system based on linux.I am supposed to the read the configuration file and edit another file.
presently I would like to read from the configuration file.It would be having values file one below.
There is chance of entering duplicate variable in configuration file however I need to extaract only the last entered value.
example file.conf
ChangeVersion=0
IPAddress=192.168.1.195
GATEIPAddress=192.168.1.1
WebServerIP=192.168.1.106
IPAddress=192.168.1.201
IPAddress=192.168.1.223
I need to extarct the change version,GATEIPAddress and IPAddress which comes as the last entry. How we can achive this.
Thanks in Advance,
Ratheendran
clx
February 28, 2011, 7:46am
2
You could source the file. the duplicates will always have the last entry since it overwrites the previous entry.
. file.conf
or
source file.conf
See if this would work for you:
sed -n -e '/ChangeVersion/p' -e '/GATEIP/p' -e '$p' inp_file
ctsgnb
February 28, 2011, 11:20am
4
# cat tst
ChangeVersion=0
IPAddress=192.168.1.195
GATEIPAddress=192.168.1.1
WebServerIP=192.168.1.106
IPAddress=192.168.1.201
IPAddress=192.168.1.223
# nawk '/^IPAddress/{x=$0}/GATE/||/Version/{print}END{print x}' tst
ChangeVersion=0
GATEIPAddress=192.168.1.1
IPAddress=192.168.1.223
# nawk -F= '/ChangeVersion/||/IPAddress/{A[$1]=$2}END{for(i in A) print i"="A}' tst
GATEIPAddress=192.168.1.1
ChangeVersion=0
IPAddress=192.168.1.223
if it is sure that IPAddress is in last line then
# nawk '{x=$0}/GATE/||/Version/END{print x}' tst
ChangeVersion=0
GATEIPAddress=192.168.1.1
IPAddress=192.168.1.223
Thanks every one for your response.
I guess I need to make the requirements more clear.
I would like to get the values from config file and store them to a variable,since the chances of entering the duplicate fields are obvious.
#cat tst
WebServerIP=192.168.1.106
ChangeVersion=0
IPAddress=192.168.1.195
GATEIPAddress=192.168.1.1
GATEIPAddress=192.168.1.102
WebServerIP=192.168.1.106
IPAddress=192.168.1.201
IPAddress=192.168.1.223
HiSpeedNet=1
HiSpeedNet=2
so I need to retrieve last entered field and store it in a variable for subsequent manipulation.
e.g HiSpeedNet=2, IPAddress=192.168.1.223, GATEIPAddress=192.168.1.102 ..etc.
Could this help you ?
awk -F"=" '{b[$1]=$0;next} END{for(i in b) {print b}}' testfile
The first response is the answer you're looking for. See post #2 by anchal_khare.
Regards,
Alister