@anubhava's answer is a great in that if you know the pattern will be in the last X lines you can use tail -n X
. However if you don't know the X value you have a couple of options.
1) Instead of trying to modify the tac command, modify the grep command to only match the first (this saves computational time because grep doesn't have to process the whole file).
tac accounting.log | grep -m 1 $pattern
2) Write a for
loop that processes the file line by line backwards and then breaks from that for loop once you have found what you want.