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 Pattern2:TheRightBar
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?
sed '/First Pattern/q'would be better thansed -e '/First Pattern/,$d'sosedstops reading at the first occurrence. – Stéphane Chazelas Oct 04 '16 at 14:38