while( ) { my $in_toc = 1 .. /^={5,}/; if( !$in_toc && /^Chapter 2\./ .. /^Chapter 3\./ ) { next if /^Chapter \d+\./; print; } } __DATA__ Table of Contents Chapter 1. Introduction Chapter 2. Main Chapter 3. Conclusion ============================== Chapter 1. Introduction This is the introduction preceding Chapter 2. Chapter 2. Main This is the text contained in Chapter 2 and will contain a lot of text with at least 100 words and probably somewhere around 1000-5000. Chapter 3. Conclusion This is the conclusion.