Add split sections script for plain text
[project-aon.git] / common / scripts / split-sections.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use autodie;
5 use warnings qw< FATAL all >;
6 use IO::Handle;
7 use Scalar::Util qw< openhandle >;
8
9 #Initialise variables
10 my $debug = 1;
11 my $fh = new IO::Handle;
12 my $line = "";
13 my $section = "";
14 my $filename = "";
15
16 while ($line = <STDIN>) {
17     chomp ($line);
18     print STDERR "DEBUG: Reading '$line'\n" if $debug;
19     if ($line =~ /^(\d+)$/) {
20         $section = $1 ;
21         $filename = "$section.txt";
22         print STDERR "DEBUG: Starting section $section\n" if $debug;
23         close $fh if $fh->opened();
24         open ($fh, ">>$filename") or die "Couldn't open file $filename, $!"
25     } 
26
27     # Print to the file if we have a filehandle except for the 
28     # section number itself
29     if ($line !~  /^(\d+)$/) {
30         if ( $fh->opened() ) {
31             print STDERR "DEBUG: Printing to $filename\n" if $debug;
32             print $fh $line."\n" ;
33         }
34     }
35 }
36
37
38 close $fh if fileno($fh);
39
40 exit 0;