Reputation: 1239
I would like to extract the Bcast Ip Address from dhcp-range line which is specified in the server configuration file.
dhcp-range=192.168.42.1,192.168.42.253,255.255.255.0,192.168.42.255,24h
I would like to store in a variable 192.168.42.255
Actually I am using
DNSMASQ_DIR="dnsmasq.conf"
if [ -e "$DNSMASQ_DIR" ]; then
BcastAddress=`sed -n 3p $DNSMASQ_DIR | awk '{print $1}'`
b=${BcastAddress:53:66}
echo $BcastAddress
echo $b
else
echo "file not exists"
fi
which returns :
dhcp-range=192.168.42.1,192.168.42.253,255.255.255.0,192.168.42.255,24h
192.168.42.255,24h
I don't understand why ,24h is included. At the same time, I think that my method will be not efficient if the other IP addresses change. Is there a more efficient way to resolve my issue ?
Upvotes: 0
Views: 93
Reputation: 5092
You can refer cut command also
cut -d',' -f4 dnsmasq.conf
Example :
DNSMASQ_DIR="dnsmasq.conf"
if [ -e "$DNSMASQ_DIR" ]; then
b=`sed -n 3p $DNSMASQ_DIR | cut -d',' -f4 `
echo $b
else
echo "file not exists"
fi
Sed command printed 3rd line and cut command extract the 4th column delimiter of ,
Output :
192.168.42.255
Upvotes: 2
Reputation: 26667
If you want to do it in awk
$ echo "dhcp-range=192.168.42.1,192.168.42.253,255.255.255.0,192.168.42.255,24h" | awk -F, '{print $4}'
192.168.42.255
Simplified version of your code
You can even simplify the entire script using awk as
$ awk -F, '/^dhcp-range/{print $4}' dnsmasq.conf
192.168.42.255
-F,
sets the field seperator as ,
/^dhcp-range/
selects line starting with dhcp-range
print $4
prints the fourth column`
OR
$ awk -F, 'NR==3{print $4}' dnsmasq.conf
192.168.42.255
NR==3
selects the third lineUpvotes: 1