Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Extracting Header

by raj8 (Sexton)
on Aug 06, 2002 at 18:42 UTC ( #188115=perlquestion: print w/replies, xml ) Need Help??
raj8 has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I have been attempting to open a file and cut out the header which covers about 3 lines in the file. At that point I just extract the relevant information. However, I can't find a function to cut this header out..

Replies are listed 'Best First'.
Re: Extracting Header
by kvale (Monsignor) on Aug 06, 2002 at 18:49 UTC
    I'm not sure what you mean by cut out the header, but here is a way to ignore it. Suppose header lines start with an octothorpe  # :
    open FILE, "<file.txt" or die "Couldn't open file.txt: $!"; while (<FILE>) { if (/^#/) { # ignore the header } else { # Do some processing } }
    If your goal is to remove the header from the file, the easiest approach is to copy from the original file to a new file, leaving out the header lines in the process.


      For Example:
      media media robot robot robot side/ ------------------------------------------ 000381 8MM NONE&nbssp; 12 06/20/2002 16:44 000443 8MM TL8 0 2 11/02/2001 13:12

        I'm guessing that you want to "cut" the header lines out of the file, editing the file and leaving it with only the data in it. Here's the gradual evolution of something that will do that:

        First off, we need to get a general plan. We want to read in the file line by line, and make a new file as we do so -- the trick is to not print the line if it's a header line. This means our new file will only have the data in it. So here's something that does that:

        #!/usr/bin/perl -w; open IN, "" or die " $!"; open OUT, ">file.out" or die ">file.out: $!" while (<IN>) { if ($_ =~ /^\d/) { print OUT $_; } } close IN; close OUT;

        Next step is to make this code a little classier. First off, we can use the -n and -i command line options to make this a lot shorter. The -n does the work of the while loop, and the -i does the word of making a new file, and then replacing the original. It'll even keep a backup for you! Here's what it looks like using -ni, then:

        #!/usr/bin/perl -ni.bak if ($_ =~ /^\d/) { print $_; }

        ..Which can be condensed and made a lot more canonical:

        perl -ni.bak -e 'print if /^\d/' file.dat


        Update: Manythanks to ChemBoy. I said -ni in two places, then gradually drifted off to saying -pi. Oopsie.

        perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://188115]
Approved by kvale
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-07-21 02:25 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (443 votes). Check out past polls.