Is it possible using awk
or sed
to get the line number of a line such that it is the first line matching a regex after another line matching another regex?
In other words:
- Find line l1 matching regex r1. l1 is the first line matching r1.
- Find line l2 below l1. l2 matches regex r2. l2 is the first line matching r2, ignoring lines l1 and above.
Clarification: By match I mean partial match, for most general solution.
A partial match can of course be turned into a full-word match with \<...\>
or a full-line match with ^...$
.
Example input:
- - '787928'
- stuff
- - '810790'
- more stuff
- - '787927'
- yet more stuff
- - '828055'
- some more stuff
- - '828472'
- some other stuff
If r1 is ^-.*787927.*
and r2 is ^-
I'd expect the output to be 7, i.e. the number of the line that says - - '828055'
.
pattern
as it's highly ambiguous. Instead use the wordregexp
orstring
, whichever it is you mean, and clarify if you want partial, full-word, or full-line matches or something else. Additionally - whatever it is you're trying to do post concise, testable sample input and expected output that full covers all your requirements. β Ed Morton