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

dear monks, I have a simple question that seems to be baffling me. I Have a text file that I have to process that is generated from anther source. There are 5 to 6 lines on top and 3 to 4 lines on the bottom I want to delete. I am having trouble unlinking the lines so as the page is clean from thelm. Can someone help me please.
ex. raw file line 1 line 2 line 3 data data data data line 4 line5 need the file to look like this data data data data data
Please help

Replies are listed 'Best First'.
Re: text manipulation
by BMaximus (Chaplain) on Apr 09, 2001 at 08:53 UTC
    I have a great book for you that has the answer to this one. Its called the Perl Cookbook. Its worth its weight in gold for stuff like this.

    One of the examples in Chapter 7 should do it for you.
    7.10 Modifying a file in place without a temporary file

    open(FH, "+< FILE") or die "Can't open file: $!"; @file_array = <FH>; # perverse, abuse, and do stuff to array here seek(FH, 0, 0); print FH @file_array; truncate(FH,tell(FH)); close(FH);

    Just throwing out a suggestion. There are a dozen or so ways to do this.

    Good luck,
    BMaximus
Re: text manipulation
by twerq (Deacon) on Apr 09, 2001 at 06:56 UTC
    Do you know exactly how many lines you want removed from the top and bottom of the file? If so, it can be as easy as
    # read all the lines in the file into an array @all_lines = (<INFILE>); # grab only the lines you want @stripped_lines = splice(@all_lines, 3, scalar(@all_lines)-5); # output to the new file print OUTFILE @stripped_lines;
    Otherwise, if you need to evaluate the data before knowing which lines to remove, you're looking at a regular expression.
Re: text manipulation
by cLive ;-) (Prior) on Apr 09, 2001 at 08:47 UTC
    anther - The pollen-bearing part of the stamen.

    Just curious how that generated a text file :)

    cLive ;-)

      Must be a genetic algorithm!

      --
      I'd like to be able to assign to an luser