finding the last item

finding the last item

am 22.01.2008 22:09:09 von paintedjazz

Is there a way to search for the last occurrence of a string in a
file? I want to find the last occurrence of an IP address associated
with its MAC address in /var/log/dhcpd.log -- any suggestions other
than:

grep "111.222.333.444 to 00.11.22.33.44.55" /var/log/dhcpd.log |
tail -1

Re: finding the last item

am 22.01.2008 22:17:46 von Ed Morton

On 1/22/2008 3:09 PM, paintedjazz@gmail.com wrote:
> Is there a way to search for the last occurrence of a string in a
> file? I want to find the last occurrence of an IP address associated
> with its MAC address in /var/log/dhcpd.log -- any suggestions other
> than:
>
> grep "111.222.333.444 to 00.11.22.33.44.55" /var/log/dhcpd.log |
> tail -1
>

awk '/111.222.333.444 to 00.11.22.33.44.55/{f=$0} END{print f}' /var/log/dhcpd.log

or:

tac /var/log/dhcpd.log | grep -m 1 "111.222.333.444 to 00.11.22.33.44.55"

Regards,

Ed.

Re: finding the last item

am 22.01.2008 23:00:51 von Andrew Smallshaw

On 2008-01-22, paintedjazz@gmail.com wrote:
> Is there a way to search for the last occurrence of a string in a
> file? I want to find the last occurrence of an IP address associated
> with its MAC address in /var/log/dhcpd.log -- any suggestions other
> than:
>
> grep "111.222.333.444 to 00.11.22.33.44.55" /var/log/dhcpd.log |
> tail -1

There isn't really anything wrong with this grep|tail approach in
terms of its efficiency. Even if you wrote a custom C app for this
you would still have to follow the same basic steps - namely find
all matching lines and then isolate the last one. I see that Ed
Morton has given you a few alternatives, the awk one being a
particularly natural programmer approach, but ultimately I doubt
you would find either suggestion is noticeably faster in practice
- indeed I suspect the tac based approach would actually be slower.

--
Andrew Smallshaw
andrews@sdf.lonestar.org

Re: finding the last item

am 23.01.2008 08:21:14 von Stephane CHAZELAS

On Tue, 22 Jan 2008 23:00:51 +0100 (CET), Andrew Smallshaw wrote:
> On 2008-01-22, paintedjazz@gmail.com wrote:
>> Is there a way to search for the last occurrence of a string in a
>> file? I want to find the last occurrence of an IP address associated
>> with its MAC address in /var/log/dhcpd.log -- any suggestions other
>> than:
>>
>> grep "111.222.333.444 to 00.11.22.33.44.55" /var/log/dhcpd.log |
>> tail -1
>
> There isn't really anything wrong with this grep|tail approach in
> terms of its efficiency. Even if you wrote a custom C app for this
> you would still have to follow the same basic steps - namely find
> all matching lines and then isolate the last one. I see that Ed
> Morton has given you a few alternatives, the awk one being a
> particularly natural programmer approach, but ultimately I doubt
> you would find either suggestion is noticeably faster in practice
> - indeed I suspect the tac based approach would actually be slower.

No, for a big file, tac will be faster, as it does read from the
end and does some lseeks to walk the file backward (as long as
the file is a regular file which is probably the case for
/var/log/dhcpd.log).

But note that tac is a GNU command and not a Unix command. So
is the -m grep option.

Other than that, I agree that grep | tail -n 1 is probably the
best POSIX approach in terms of efficiency and legibility.

--
Stephane