#!/usr/bin/perl require("cgi-lib.pl"); &ReadParse; #This program prints data from a csv file. The file can be compressed. #You need to know the delimter (default is comma), as well as the variable #names. By default it will print the first 10 lines of data, but you can #print other ranges. #Written by Paul H. Bern 26JAN01 print "\nEnter file name: "; $f=<>; chomp($f); print "\nEnter variables to print: "; $v=<>; chomp($v); print "\nEnter delimiter[,]: "; $d=<>; chomp($d); if($d eq "") {$d=",";} print "\nEnter lines to print[1 10]: "; $nl=<>; chomp($nl); print "\n\n"; if($f =~ /gz$|Z$/i) { open(inp,"zmore $f|") or die "file not found"; $fl=2; if($nl eq "") { $s=3; $e=12; } if($nl ne ""){ @nl=split(/ /,$nl); $s=@nl[0]+2; $e=@nl[1]+2; } } if($f !~ /gz$|Z$|z$|zip$/i) { open(inp,"$f") or die "file not found"; $fl=1; if($nl eq "") { $s=2; $e=11; } if($nl ne ""){ @nl=split(/ /,$nl); $s=@nl[0]+1; $e=@nl[1]+1; } } @varnames=split(/ /,$v); while () { @col=split(/$d/,$_); if($. == $fl) { for($j=0; $j<=@col; $j++) { for($i=0; $i<@varnames; $i++) { if (@col[$j] =~ /^@varnames[$i]$/i) { push(@vars,$j); if (@varnames[$i] ne @varnames[-1]) { if(@varnames>1) {print "line\t";} print "@col[$j]\t"; } if (@varnames[$i] eq @varnames[-1]) { if(@varnames==1) {print "line\t";} print "@col[$j]\n"; } } } } } #end if 1 if($. >= $s && $. <= $e) { if($fl==1) {$l=$.-1;} if($fl==2) {$l=$.-2;} foreach $v (@vars) { if ($v ne @vars[-1]) { if(@varnames>1) {print "$l\t";} print "@col[$v]\t"; } if ($v eq @vars[-1]) { if(@varnames==1) {print "$l\t";} print "@col[$v]\n"; } } } if($. == $e) {print "\n\nHit cntrl-c\n\n";} } #end close(inp);