Science Gateway > Protocols > Cell Biology Protocols - Table of Contents

Pubplus.org - a PubMed search engine
Star Republic: Guide for Biologists

Perl Example 1 --- file preview

Data files from high throughput experiments, such as whole genome sequences, are frequently too big to open with a text editor. The following Perl script reads the first few lines of the file and print the content to the screen, allowing user to peek into the file.

#!perl

#input name of the file to be read from command line

$infile = @ARGV[0];

#number of lines to display, default 20
$noLinesToRead = @ARGV[1];

#remind user there is no input file
if(!$infile) {
  print "No input file.\nUsage: perl preview.pl filename noLinesToRead\n";
}

#if no user input of number of lines to read, set default to 20
$noLineToRead = 20 if(!$noLinesToRead);

#read the input file if given
if($infile) {
  
  #initiate line count
  $lineNo = 0;
  
  # open file
  open(IN, $infile) or die "can not open $infile\n";

  #read the first few lines of the file
   while(  $lineNo < $noLinesToRead  && ($line = <IN>) ) {
     chomp($line);
     print "$line\n";

     #count number of lines read
     $lineNo++;
   }

  close(IN) or die "can not close $infile\n";

}