#!/usr/local/bin/perl # process csv files from camitro # read in file one row at a time, reporting on contents # write data files in format appropriate for Tpros # Default settings $inputmin=4; # start at col 4 $inputmax=0; # use whole row. $target=3; # target column (alternate is 2) $comments = 0 ; $blank = 0 ; $lastnum = -999 ; $num_lines_same = 0 ; $num_blocks = 0 ; $sep="0"; # replace by preferred separator $sep=","; # replace by preferred separator $i="in"; $t="ta"; $skip = 1 ; # number of header lines to drop eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # Prepare to read and write if(0 eq $inputmax) { $autoinputmax = 1 ; } else { $autoinputmax = 0 ; } $target -- ; # zero offset column convention $inputmin -- ; # zero offset column convention if($target>=0) { open ( TA , ">$t" ) ; } open ( IN , ">$i" ) ; # Read and write $line = 0 ; while ( <> ) { $line ++ ; s/^\s*//; # get rid of initial gap on line s/\n//; # get rid of newline if ( /^\#/ || /^\%/ || ($line<=$skip) ) { $comments ++ ; } else { if("0" eq $sep) { @w=split ; # use default space separator }else{ @w=split($sep, $_) ; } if ( $#w == -1 ) { $blank ++ ; } else { if ( $#w != $lastnum ) { # new block if ( $num_blocks > 0 ) { # report previous block &report () ; } $num_blocks ++ ; $num_lines_same = 0 ; $lastnum = $#w ; if ($autoinputmax) { $inputmax = $lastnum ; } } $num_lines_same ++ ; if ($ta>=0) { print TA $w[$target]."\n" ; } if($inputmax>=$inputmin) { print IN join(" ",@w[$inputmin..$inputmax])."\n" ; } } } } &report () ; if ( $blank + $comments > 0 ) { print "-----------\n$blank blank lines, $comments comments\n" ; } sub report { $lastnum ++ ; print STDERR "Block $num_blocks: $num_lines_same lines, $lastnum entries\n" ; }