Searching dhcp.leases for an IP (by MAC)

Jeff Schroeder jeff at
Tue Dec 12 09:32:00 MST 2006

Chris asked:

> I need to write a function to search dhcpd.leases file to find IP
> addresses (I have the corresponding MAC address). This would be easy
> for me if the file had each lease on one line. In that case I could
> just grep for the MAC address , then parse out the IP address.
> However, the IP address is 5 lines above the said MAC address:

You may be able to do this with 'sed'.  

# print the line immediately before a regexp, but not the line
# containing the regexp
sed -n '/regexp/{g;1!p;};h' file

Off the top of my head I can't figure out how to print the line 5 lines 
previous to the regexp.

Another method, mentioned by others here, is to use grep's -B to get n 
lines of context.  You could combine that with head, thus:

grep -B 5 MAC file | head -1

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : 

More information about the PLUG mailing list