I have a large text document that contains multiple reports in it. I want to be able to extract each report from the large text file and write them all to their own files. For example, say the text of the large file looks like this:
Report 1
this is just some random text for example purposes.
text text text text text text text text text text
Report 2
this is just some random text for example purposes.
text text text text text text text text text text
Report 3
this is just some random text for example purposes.
text text text text text text text text text text
There are just over 1200 reports in this one file and I need to be able to split them up into their own files. I have one script that is able to identify the beginning of each report, but I do not understand quite yet how to tell Perl to identify it, extract what is below it until it finds the instance of another report, and then write what it has to its own file. Below is code that I wrote that is able to identify the report. If someone could offer advice on how I could modify this code to extract each report, I would greatly appreciate it.
Again, this merely counts each instance of "Report *" which tells me exactly how many reports are in the file. Just want to extract each one to its own file. Thanks in advance for any help!
Report 1
this is just some random text for example purposes.
text text text text text text text text text text
Report 2
this is just some random text for example purposes.
text text text text text text text text text text
Report 3
this is just some random text for example purposes.
text text text text text text text text text text
There are just over 1200 reports in this one file and I need to be able to split them up into their own files. I have one script that is able to identify the beginning of each report, but I do not understand quite yet how to tell Perl to identify it, extract what is below it until it finds the instance of another report, and then write what it has to its own file. Below is code that I wrote that is able to identify the report. If someone could offer advice on how I could modify this code to extract each report, I would greatly appreciate it.
Code:
while(1) #loop until user quits
print "\n\n\nFile To Be Processed: ";
$File = <STDIN>;
print "\n\nOutput File Name: ";
$Outfile = <STDIN>;
chomp($File);
open(INFILE, $File) or die "The file cannot be found.";
open(OUTFILE, ">>$Outfile");
count = 0
while(<INFILE>) {
chomp($_);
if(/(Report \d+)/) {
count++;
}
}
print "There were $count reports in this document."
print OUTFILE "$1\n"
close INFILE
close OUTFILE
Again, this merely counts each instance of "Report *" which tells me exactly how many reports are in the file. Just want to extract each one to its own file. Thanks in advance for any help!