dkhalfe has asked for the wisdom of the Perl Monks concerning the following question:

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"; } # } } } }

Replies are listed 'Best First'.
Re: Obtaining header from file (first line) - printing to output file
by roboticus (Chancellor) on Jul 16, 2012 at 21:18 UTC

    dkhalfe:

    The easiest way I can think of to put the first line into a file is, as you alluded to: head -1 thefile >thefile.firstline. Doing it in a perl one-liner is nearly as easy: perl -pe 'last if $.==2' thefile >thefile.firstline.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Obtaining header from file (first line) - printing to output file
by kcott (Archbishop) on Jul 17, 2012 at 05:05 UTC

    Does changing

    print OUTFILE head -1;

    to

    print OUTFILE $line if $. == 1;

    do what you want?

    If you're unfamiliar with $., it's documented in perlvar.

    -- Ken

      or simply pumping in the entire file once(though not a good practice) and then filtering the first line.
      my @filelines=`cat $input_file`;print OUTFILE $filelines[0];
Re: Obtaining header from file (first line) - printing to output file
by frozenwithjoy (Priest) on Jul 17, 2012 at 03:00 UTC
    It isn't entirely clear to me what you are asking; however, I'm assuming that your input file has column headers that you want to keep for your output file, but you want to strip them off the input file before processing it. Is this correct? If so, you were sort of on the right track with the code you commented out towards the beginning:
    #head -1 <IN>; #my $firstline = IN;

    Try changing this to the following and see if it does what you want:

    my $header = <IN>; print OUTFILE $header;

    Of course, be sure to open your OUTFILE file handle before writing.

Re: Obtaining header from file (first line) - printing to output file
by frozenwithjoy (Priest) on Jul 16, 2012 at 20:12 UTC
    Hi. It looks like you used 'code' instead of '/code' to end your code block. If you edit that, your post will be a lot easier to read. Thanks.
Re: Obtaining header from file (first line) - printing to output file
by Anonymous Monk on Jul 16, 2012 at 22:44 UTC
    Could you please post a 10 lines or so of your data. Also include the first line. Maybe we could better discern your problem.

    Please edit your post, you need only make 1 change. Your closing code tag is <code>. That should be </code> with the forward slash.

    It is impossible to read your code the way it is now.

      Sorry, I was vague on your problem. You want to print the forst line to a file, then do some additional processing. You could put the print statement in the section that tests for $x == 1. (You could have used the file line number variable, $. instead of $x. $. starts with 1 to the end.)
      if ($x==1) { print OUTFILE $line, "\n"; (@keys)=split(/\t/, $line); } else { my $y=0; (@holder) = split(/\t/, $line); my %hash; for my $column (@holder) {
        Thanks, Ill give it a try tomorrow and let you know how all goes!