Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter text which appears between two marks

Part 1

What is the easiest way to create a text filter which outputs only text surrounded by two predefined marks. I don't mind using any standard tool: sed, awk, python, ...

For example, i would like only the text surrounded by "Mark Begin" and "Mark End" to appear.

input:
Text 1
Mark Begin
Text 2
Mark End
Text 3
Mark Begin
Text 4
MarK End
Text 4

output:
Text 2
Text 4

Part 2

How can the solution be modified so that only the last occurrence will be written to output, so for the same input above, we get:

output:
Text 4
like image 553
zr. Avatar asked Apr 13 '26 07:04

zr.


1 Answers

$ awk '/Mark End/{f=0}/Mark Begin/{f=1;next}f' file
Text 2
Text 4

$ awk '/Mark End/{f=0}/Mark Begin/{f=1;next}f{p=$0}END{print p}' file
Text 4
like image 196
ghostdog74 Avatar answered Apr 15 '26 19:04

ghostdog74



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!