Hi, I know that you can use the head -1 command in unix to print the first line of a file. I have a data file and I need to read in the file, print the header of the input file to an output file, then do a number of other things aside from this. What command or function can I use to print the header line to an output file. You probably do not need to see my code but it is included for your reference. I don't want to change my code, rather just figure out a simple way to print the header of my input file to an output file. Thanks.

#!/usr/bin/perl #use strict; use warnings; #print "Enter the input file name: "; #my $filename = <STDIN>; #chomp ($filename); @ARGV == 1 or die "Invalid number of arguments. Please supply as argum +ents: \n1) the input file listing all data to be used."; my ($input_file) = @ARGV; open (IN, "$input_file") or die "Cannot open file: $!"; #head -1 <IN>; #my $firstline = IN; my $x=0; my @keys; my @holder; my @array_hash; my $threshold=.03; my $blank = "-"; my $nonsyn = "nonsynonymous"; open (OUTFILE, ">TEST_$input_file") or die "Cannot create an output fi +le: $!"; while(my $line = <IN>) { print OUTFILE head -1; chomp($line); $x++; if ($x==1) { (@keys)=split(/\t/, $line); } else { my $y=0; (@holder) = split(/\t/, $line); my %hash; for my $column (@holder) { $hash{$keys[$y]}=$column; $y++; } # -ne print OUTFILE; exit; # my $first = shift (@array_hash); # print OUTFILE $first; if ($hash{"1000_Genomes"} ne $blank) { if ($hash{"1000_Genomes"} <= $threshold) { if ($hash{"Syn/Nonsyn"} eq $nonsyn) { print OUTFILE $line, "\n"; } } } if ($hash{"1000_Genomes"} eq $blank) { # if ($hash{"1000_Genomes"} <= $threshold) # { if ($hash{"Syn/Nonsyn"} eq $nonsyn) { print OUTFILE $line, "\n"; } # } } } }

In reply to Obtaining header from file (first line) - printing to output file by dkhalfe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.