in reply to Grep the first occurrence of a string above a reference string
Your example suggests that the entire search is to be performed on data appearing once in a single line. If so, you have a fine suggestion above.
But if your problem case involves data that is multi-line with more than one instance of each target and reference datum (or similarly, if the data is all on a single line but may contain repetitions of the target and reference data) -- perhaps like this:
YYY AAA BBB CCC AAA freida XXX GGG FFF AAA. XXX ozymandis BBB...
... then you'll need to inspect each line for your initial string, and save it (and perhaps, its index if that's going to be useful as you go forward, which seems likely as you've stated your question) or a reference (etc) before continuing to look for your "reference data" discarding any intervening data.
Then process the saved hit (AAA) before continuing the search for additional instances of AAA before XXX.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Grep the first occurrence of a string above a reference string
by shmem (Chancellor) on May 05, 2016 at 14:16 UTC |