Sample Header Ad - 728x90

How to catch all lines of a repeating pattern and do some actions with the subresults

-3 votes
4 answers
192 views
I am looking for a possibility to catch in a repeating text pattern all variable amount of lines between them and then do an action with it in bash. Example text:
Total:
text1
text2
Total:
text3
Total:
Text1
Text4
Text5
What I am aiming to do is basically a for loop over the matches with Total: and then do an action with it, which is always going to be the first section of the follow-up subtext. Something like in high level language: for (cat filename = every "Total:" do end Now the interesting part for me is basically how to organize that for loop? In the ` part I want do some jq and awk`. The results would be basically based on the example text these three matches: 1.
Total:
text1
text2
2.
Total:
text3
3.
Total:
Text1
Text4
Text5
Hope the last description describes it. What could be the right tool for catching this? Would that be rather a combination of for and grep or for and awk? I would like to use no more than GNU tools. So no perl or other external tools. Thanks a lot.
Asked by André Letterer (9 rep)
Apr 2, 2024, 04:01 PM
Last activity: Apr 3, 2024, 12:34 PM