Print text between 2 strings for the entire file

hey guys,

for the following output:

starting
open
open
close
close
starting
close
starting
open
close
close
starting
open
open
close
open

i m able to get the text between "starting" and "close" and move them to a diffrent file like so:

sed -n '/starting/{:L;N; /close/bK; bL; :K;p;q}' >>somefile.txt 

somefile.txt:

open
open
close
close
close

my question is how do i go about doing so for the entire file?
any help will be great
Thanks!!!

boaz733,
See this link on several different solutions for printing lines between 2 strings:

If the file you labeled as "for the following output:" is your input file, then your output should look like this:

awk '/starting/ {flag=1;next} /close/{flag=0} flag {print $0}' inputfile
open
open
open
open
open
1 Like

i actully managed to solve it with a loop:
it might be ugly but it works.

#!/bin/bash
x=`wc -l < datafile`
while [ $x -ge 1 ]
do
sed -n '/starting/{:L;N; /close/bK; bL; :K;p;q}' datafile>>outputfile
last_line=`grep -n -m2 starting datafile| tail -1 | cut -f1 -d:`
last_line=$(($last_line - 1))  
sed -i '1,'$last_line'd' datafile
x=`wc -l < datafile`
done

Output is :

starting
open
open
close
starting
close
starting
open
close
starting
open
open
close