Search the last instance of a string in a file

I have a big database log file which keepsgrowing daily. OS is HP UX. Here is a small part of it:

            Tue Jan 27 04:03:22 2009

[2009/01/27@04:03:22.906-0500] : add session begin for mfgeb on /dev/pts/th.
[2009/01/27@04:03:22.909-0500] : Converting relative path to absolute path.
[2009/01/27@04:03:22.967-0500] : add session end.
Tue Jan 27 04:03:29 2009
[2009/01/27@04:03:29.345-0500] : list session begin for mfgeb on /dev/pts/th.
[2009/01/27@04:03:29.356-0500] : list session end.
Tue Jan 27 04:03:46 2009
[2009/01/27@04:03:46.262-0500] : add session begin for mfgeb on /dev/pts/tb.
[2009/01/27@04:03:46.331-0500] : add session end.
Tue Jan 27 04:03:49 2009
[2009/01/27@04:03:49.964-0500] : list session begin for mfgeb on /dev/pts/tb.
[2009/01/27@04:03:49.972-0500] : list session end.
Tue Jan 27 04:04:32 2009
[2009/01/27@04:04:32.147-0500] : add session begin for mfgeb on /dev/pts/tg.
[2009/01/27@04:04:32.182-0500] : add session end.

I need to search last instance of string "add session begin" in the file and then print date from that line. In the case here, it's 27th Jan 2009.
Someone please help.

If you have the command "tac", just do:

tac FILE |awk '/add session begin/ { print $1 }' | head -1

Otherwise, you can do this

awk '/add session begin/ { lastseen=$1; } END { print lastseen }' FILE

I think "rev" should be "tac" - rev reverses the order of characters
From the man page:

tac reverses the order of the lines, printing the last line first.

Another possible solution (a long n crude one though :slight_smile: ) :
grep -i "add session begin" a.txt|tail -1|cut -c 2-11
where,
a.txt = sample log entry posted in the original query

"rev" reverse the order of characters line-wise

"tac" is not available on HP UX

Gaurav,
Thanks friend, your solution is working fine.