I have a huge file contains two types of patterns say pattern1 and pattern2, pattern1 may appear many times before pattern2 appears. I want to grep the last occurrence of each pattern1 before each pattern2.
Input file:
some text
pattern1=1
some lines
pattern1=2
some lines
pattern1=3
some lines
pattern2
some lines
pattern1=4
some lines
pattern1=5
some lines
pattern1=6
some lines
pattern1=7
some lines
pattern2
Desired output:
pattern1=3
pattern1=7
I tried with grep
when I know the numbers of lines between pattern2 and the previous pattern1:
grep -B400 "pattern2" | grep "pattern1"
but I need a unique command that can be run over any file regardless of the number of lines between the two patterns.
pattern 1
that appears before any instance ofpattern 2
regardless of what other strings are between the two of them, yes?pattern
everywhere in your question with whatever you really want to match - full or partial + word or line + string or regexp + anchored or not. Right now the answers you have assume you want unanchored partial line regexp matches which seems unlikely to be the most robust solution for you but that's based on what your code does since you haven't yet stated/shown what you actually need.