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
HTH,
Jeff
pgp4XbxdyssC5.pgp
Description: PGP signature
/* PLUG: http://plug.org, #utah on irc.freenode.net Unsubscribe: http://plug.org/mailman/options/plug Don't fear the penguin. */
