Best way to skip a header when reading in from a text file in Perl?

后端 未结 7 1627
难免孤独
难免孤独 2021-02-01 10:16

I\'m grabbing a few columns from a tab delineated file in Perl. The first line of the file is completely different from the other lines, so I\'d like to skip that line as fast a

7条回答
  •  耶瑟儿~
    2021-02-01 10:55

    I had a similar question/issue. My solution was the following - for either unzipped or gzipped files:

    print STDERR "\nReading input file...\n";
    if ($file =~ /.gz$/) {
        open(IN, "gunzip -c $file | grep -v '##' |") or die " *** ERROR ***     Cannot open pipe to [ $file ]!\n";
        } else {
            open(IN, "cat $file | grep -v '##' |") or die " *** ERROR ***     Cannot open [ $file ]!\n";
    }
    

    I don't know about benchmarking, but it works fine for me.

    Best,

    Sander

提交回复
热议问题