In a file like this one :
... Pattern2:TheWrongBar foo Pattern2:TheRightBar foo First Pattern foo ...
I need to find the last occurrence of
Pattern2 that is before
First Pattern which would be in this case
My first idea is to get all the remaining file before
First pattern with :
sed -e '/First Pattern/,$d' myfile | tac | grep -m1 "Pattern I need to get"
Isn't there a way to optimise this code?