sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like:

START
03:11:30 a
03:11:40 b
END
START
03:13:30 eee
03:13:35 fff
END
jjjjjjjjjjjjjjjjjjjjj
START
03:14:30 eee
03:15:30 fff
END
ggggggggggg
iiiiiiiiiiiiiiiiiiiiiiiii

I want the below output

START
03:14:30 eee
03:15:30 fff
END

Basically i want to display the lines between START and END of last match occurrence

Welcome to the forum.

Any attempts / ideas / thoughts from your side? Does it have to be sed ?

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did not post homework, please explain the company you work for and the nature of the problem you are working on. And, please also tell us the operating system and shell you're using. And, as RudiC asked, why sed ? This is much easier to do with ed or ex .

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

I have tried below. But its displaying all lines between START and END

sed -n '/START/,/END/p' file.txt

I repeat: Is this a homework assignment?

And, for the third time: Why is sed the only option for this task?

I have tried using grep and finding the line numbers of matched strings which worked but its time consuming because in the original scenario there will be thousands of lines between START and END and reading all these lines will be time taking which effects the automation. Looking to resolve it primarily through "sed" and secondarily "awk"

---------- Post updated at 05:35 PM ---------- Previous update was at 05:34 PM ----------

It's not a homework assignment. Its part of automation work for file handling.

You still haven't said why you can't use ed or ex for this. The following code is much simpler than anything you can do with awk or sed :

ed -s file.txt <<-"EOF"
	?START?;/END/p
	q
EOF

But, of course, this depends on using a shell that uses Bourne shell syntax and may be depending on a particular version of ed (depending on what OS you're using). Telling us what OS and shell you're using anything you start of thread in these forums allows people trying to help you avoid making wild assumptions about your environment and helps you get an answer that will work in your environment faster.

1 Like

It's not a homework assignment. Its part of automation work for file handling. I dont know about ed or ex.

Now that the homework question has been answered, this thread has been reopened.

See post #7 for an easy solution that works with both ed and ex .

Simple sed approach:

sed -n '/START/,/END/ {H}; /START/ h; $ {g;p}' file
START
03:14:30 eee
03:15:30 fff
END
2 Likes

I am working on linux server using a bash shell

---------- Post updated at 05:53 PM ---------- Previous update was at 05:50 PM ----------

Thanks Rudic.

can you please give explanation of this

sed -n '/START/,/END/ {H}; /START/ h; $ {g;p}' file

How H , h , g works here ?

man sed :

Thank you :slight_smile: :slight_smile: :slight_smile:

Another sed, with a loop

sed -n '
  /START/{
    :Loop
    N
    /END/!bLoop
    h
  }
  $g;$p
' file.txt