I have some sed only solutions for you:
Given this input file
$ cat file
myname
_ something
_ something
_ something
myname
_ something
_ something
myname
_ something
sed -n -e '/myname/,$ { 1,/myname/p }' file
produces this output
myname
_ something
_ something
_ something
myname
Adding | sed '$d'
will delete the last line, so
sed -n -e '/myname/,$ { 1,/myname/p }' file | sed '$d'
gives this output
myname
_ something
_ something
_ something
Explanation
sed -n
supresses line output /myname/,$
selects all the lines from myname
to the end of the file.
From the selected lines { 1,/myname/p }
will print all the lines to the next myname
and finally | sed ‘$d’
deletes the last line of the previous command.
Obviously, this will also work where first and second regexs are different.
Bonus
sed -n -e '/myname/,$ { 1,/myname/ { /myname/!p } }' file
prints this:
_ something
_ something
_ something
Why the original selection isn’t working
Originally I couldn’t understand why your script wasn’t working. I had been trying something similar myself and had been searching for reasons why it wasn’t and just assumed that sed
was, inexplicably, broken.
Then I realised: sed
processes the whole file and applies your (and my!) patterns repeatedly, it finds several examples of myname … myname
and prints all of them!
If the start and end /regexs/ are different then the expample below works (say your file had a mixture of myname and othername)
sed -n -e '/myname/,/othername/ { p; /othername/q }' file
You will still have to delete the last line of output if you don’t want the line containing othername.