in reply to how to get context between two flag

Use of / /../ / operator is a good idea!

But here I just show one simple approach that is easy to debug. There are Lot's of ways to do what you need.

Update: I just assumed that the last line sequence was all one line as you had blank lines before other examples - maybe a bad assumption - bichonfrise74's code looks good to me also - there are many ways Rome here.

#!/usr/bin/perl -w use strict; while (<DATA>) { next if /^\s*$/; #skip blank lines chomp; #optional as \n would get deleted anyway s/^.*?start\s+//; #remove start and all before s/end.*//; #remove end and all after print "$_\n"; } #prints: # asdasd asdasdasd asdasdas # as asdas dasdasdad asdasddas # asdsadsdasddasds sdasdas asdasdasdasd asdasdsa asdasd asdasdasd __DATA__ asdasd start asdasd asdasdasd asdasdas end asdasdas adasdas start as asdas dasdasdad asdasddas end qweqwe asdasd start asdsadsdasddasds sdasdas asdasdasdasd asdasdsa asdasd asd +asdasd end
Update:

sorry for goof, brain isn't working full speed today!

s/^.*?start/start/; s/end.*/end/;
preserves start and end tokens.