How to print the lines between two pattern in a file?

file like:

 services:
        XXXXXXXX:
          XXXXXXX:
           XXXXXXXXX1
         XXXXX
          XXXXXXXXX
      DDDDDDDDDDD
     BBBBB:
      version: 11.2.0.4
      services:
      YYYYYYYYYY
         XXXXXXXXXX:
           XXXXXXXXX
         XXXXXXXX
           XXXXXXXXX

I tried:

 sed -n  "/services/,/version/p"  test.txt

expect

 services:
        XXXXXXXX:
          XXXXXXX:
           XXXXXXXXX1
         XXXXX
          XXXXXXXXX
      DDDDDDDDDDD
     BBBBB:
      version: 11.2.0.4

but got all lines.

can anyone help please?

Hello netbanker,

Kindly try the following for same and let me know if this helps. Also please always use code tags in your posts for commands and codes,
go through the rules of forum on same link is as follows.

awk '/version/ {B=B?B ORS $0:$0;f=0}  /services/ {f=1;} f{B=B?B ORS $0:$0} !f{print B;B=""}'  Input_file

Output will be as follows.

services:
XXXXXXXX:
XXXXXXX:
XXXXXXXXX1
XXXXX
XXXXXXXXX
DDDDDDDDDDD
BBBBB:
version: 11.2.0.4

Thanks,
R. Singh

Singh,

Thank you so much, it did the trick!

btw, any hint why sed not working in my case?

You tell sed to print everything between services and version .
But after the version there is another services , so sed prints from that one, too, until it reaches the next version or the end of the file.
A more simple way of only printing the first block, is to exit at the block end.

awk '/services/ {f=1} f==1; /version/ {exit}' test.txt

sed does not have variables to store a state; a work-around is to read the next lines in a loop.

sed -e '/services/!d' -e ':L1' -e 'n;/version/q;bL1' test.txt

Try

awk 'function p(){if(s && /version/){ print l ; s = l = "" }}{s += /services/ ; if(s)l = ( length(l) ) ? l RS $0 : $0; p() }END{ p() }' file

Or

sed -n '/services/,$p;/version/q' file