Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing between two key words in a file

Status
Not open for further replies.

emilybartholomew

Technical User
Aug 3, 2001
82
US
I have this really enormous file that I would like to print only sections of. These sections are set off by the key words "Table" and "End Table". So the file looks like this:

[stuff I don't want to print]
Table
[stuff I want to print. there are about 50 lines]
End Table
[stuff I don't want to print]
Table
[stuff I want to print]
End Table
[stuff I don't want to print]
.
.
.

Any thoughts? Thnx
 
Code:
# one approach
open(FILE,&quot;<your_enormous_file&quot;) or die &quot;Failed to open, $!\n&quot;;
while ($line = <FILE>)
    {
    if ($line =~ /^Table/) { $switch = 'on'; next; }
    elsif ($line =~ /^End Table/) { $switch = 'off'; next;}
    
    if ($switch eq 'on') { print &quot;$line&quot;; }
    }
close FILE;

# OR, read the entire file in and cruise through it
# with a regex
open(FILE,&quot;<your_enormous_file&quot;) or die &quot;Failed to open, $!\n&quot;;
while (<FILE>){ $buffer .= $_; }
close FILE;
while ($buffer =~ /Table\n(.*?)\nEnd Table/gs) { print &quot;$1&quot;; }
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top