#!/usr/local/bin/perl -w # ---------------------------------------------------- # Summarize the *.txt Files in the DATASETS directory # ---------------------------------------------------- # © Martin Maechler, SfS, ETH Zurich #open(DOCS, "/bin/ls a*.txt |") # <<< testing : only those with a* # production: open(DOCS, "/bin/ls *.txt |") || die "I couldn't open *.txt files (in `pwd`): $!\n"; print "Summary of '*.txt' files in ". `pwd`. "-------\n"; my @Flen = ( 15, 20, 20, 30, 12); my $Form = '%-'.$Flen[0].'s| %-'.$Flen[1]. 's| %-'.$Flen[2]. 's| %-'.$Flen[3]. 's| %-'.$Flen[4]. "s\n"; my $nFields = scalar @Flen; printf $Form,"Filename(+.dat)", "TITEL", "KEYWORDS", "AnwendungsGebiet", "Verwendet"; printf $Form, '-' x $Flen[0], '-'x $Flen[1], '-'x $Flen[2], '-'x $Flen[3], '-'x $Flen[4]; my @line = @Flen; # same length FILE: while($filename = ) { chop $filename; next FILE unless -T $filename; if (!open(TEXTFILE, $filename)) { print STDERR "Can't open $filename --- continuing...\n"; next FILE; } $_ = $filename; s/\.txt$//; # $out .= ":\t"; # $out .= "\t" if length($filename) < 7+4; #-- an extra TAB if short entry $nl = 0; $line[$nl] = substr($_,0, $Flen[$nl]); LINE: while () { next LINE unless ++$nl < $nFields; #### ------- I could do better: Also look at the 'Dim:' line .. # next LINE if ($.>=4)&& !/^DIM\.(/; # last LINE if /^DIM\.(/; chop; #-- remove newline at end s/.*:\s+//; s/\s+$//; #-- remove trailing white space #print "$nl«$_»"; $line[$nl] = substr($_,0, $Flen[$nl]); } # if($nl < $nFields - 1) { print STDERR "** $. : nl = $nl \n"; for ($i = $nl; $i < $nFields; $i++) { $line[$i] = ''; } } printf $Form, $line[0], $line[1], $line[2], $line[3], $line[4]; }