Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Extract individual lines and block of text from large files

by Marshall (Canon)
on Apr 07, 2016 at 14:27 UTC ( #1159818=note: print w/replies, xml ) Need Help??

in reply to Extract individual lines and block of text from large files

I'm not sure that I completely understand the context of your problem, but a few suggestions. Make a more general expression for the 'NAME: DATE' lines and capture into a hash. You have 4 regex lines to describe the filename, perhaps just one different regex would suffice? When you find the place after the FILENAME where the data capture is supposed to happen, sometimes it works out well to call a subroutine to finish that job off. Here is some code...
#!usr/bin/perl use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys =1; my %hash; sub get_html { while (<DATA>) { if (/<html>/ .. /<\/html>/) ##see [id://525392] { chomp; push @{$hash{DATA}},$_; } } } while (<DATA>) { if ( (my ($name, $date) = m/^\s*([\w ]+):\s+([\w-]+)/)) { $hash{$name}=$date; } if (my ($filename) = m/^\s*<FILENAME>\s*(\w+ex(-)?21.*.htm)/i) { $hash{FILENAME}=$filename; get_html(); } } print Dumper \%hash; =Prints: ********* $VAR1 = { 'CENTRAL INDEX KEY' => '0000786368', 'CONFORMED PERIOD OF REPORT' => '20081231', 'DATA' => [ '<html>', 'blah ', 'smore blah', 'blahblah', ' **** BODY OF TEXT I WISH TO EXTRACT ***** +', '</html>' ], 'DATE AS OF CHANGE' => '20090331', 'FILED AS OF DATE' => '20090331', 'FILENAME' => 'v144610_ex21.htm', 'FORM TYPE' => '10-K' }; =cut __DATA__ *********** Sample text *************** CONFORMED PERIOD OF REPORT: 20081231 &#61663;------ individual +line I want FILED AS OF DATE: 20090331 &#61663;------ individual line I + want DATE AS OF CHANGE: 20090331 &#61663;------ individual line +I want CENTRAL INDEX KEY: 0000786368 &#61663;------ individual line + I want FORM TYPE: 10-K &#61663;------ individual line I want Whole buncha text here . </DOCUMENT> <DOCUMENT> <TYPE>EX-21 <SEQUENCE>7 <FILENAME>v144610_ex21.htm &#61663;-----------My starting point <TEXT> <html> blah smore blah blahblah **** BODY OF TEXT I WISH TO EXTRACT ***** </html> </TEXT> </DOCUMENT> &#61663;----------- My ending point **********End of sample text ***********

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1159818]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2022-08-11 17:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found