perl: removing the first line in HUGE files


Results 1 to 8 of 8

Thread: perl: removing the first line in HUGE files

  1. #1
    Join Date
    Jan 2002
    Location
    Michigan
    Posts
    100

    perl: removing the first line in HUGE files

    I'm working with some VERY large files (around 128M) and as I process each line in the files, I want to remove it for two reasons. First, to gradually make the file more manageable, and second, so that I can pick up where I left off should my power/connection get interrupted.

    I've done this sort of thing before using splice, but we are talking about WAY too much file I/O to do it that way.

    Is there a different way to remove the first line that is better suited to my needs?

  2. #2
    Join Date
    Nov 2002
    Location
    Stockholm/Sweden
    Posts
    106
    Something like this could probably work:
    Code:
    cat file.txt |perl -pe '$pass=0; while(<>){if($pass == 0){shift} print; $pass ++}' >newfile.txt
    Perhaps there is a more elegant solution to do it?
    ::.. Debian GNU/Linux für alle ..::
    Home | MAME Arcade | Screenshots

  3. #3
    Join Date
    Mar 2003
    Location
    Tampa, FL USA
    Posts
    2,193
    Originally posted by apeekaboo
    Something like this could probably work:
    Code:
    cat file.txt |perl -pe '$pass=0; while(<>){if($pass == 0){shift} print; $pass ++}' >newfile.txt
    Perhaps there is a more elegant solution to do it?
    Is that the same as this?
    Code:
    sed 1d file.txt > newfile.txt
    If so, it would seem to be very inefficient.

    thegreatorangepeel, I'm not fluent in perl, but perhaps it would be easier for you to reverse the file and snip from the end?

  4. #4
    Join Date
    Nov 2002
    Location
    Stockholm/Sweden
    Posts
    106
    [QUOTE]Originally posted by Sepero
    Is that the same as this?
    Code:
    sed 1d file.txt > newfile.txt


    You cheated! That's not perl...
    I'm by no means a perl guru so I think it could be done in a more simple way, but I doubt it can top sed.
    But please prove me wrong.
    ::.. Debian GNU/Linux für alle ..::
    Home | MAME Arcade | Screenshots

  5. #5
    Join Date
    Jan 2001
    Location
    Somewhere in middle America
    Posts
    164
    perl -ne 'print if $. > 1' filename > filename.new


    P.S. I think VERY big is > 1GB ... yours are just very big.
    My Machine:
    Maytag SAV5905
    710 rpm Stainless Steel Drum
    Dual boot: Gentoo / Tide

  6. #6
    Join Date
    Feb 2003
    Location
    London
    Posts
    1,022
    To get a copy of a file, without the first line of the file:
    Code:
    lines=`wc -l filename|awk '{print $1}'`
    lines=$(( $lines  - 1 ))
    tail -$lines filename > newfile
    HTH
    Cheers
    Chris

  7. #7
    Join Date
    Mar 2003
    Location
    Tampa, FL USA
    Posts
    2,193
    Programming Challenge

    Is there no possible way of cutting one line from the file without creating a separate file? (Preferably using perl)

  8. #8
    Join Date
    Nov 2002
    Location
    Stockholm/Sweden
    Posts
    106
    It's possible with the -i swith in perl.
    Using dchidelf's solution it could look like this:
    Code:
    perl -i -ne 'print if $. > 1' filename
    This would do an in-place replacement, writing back to the same file.
    ::.. Debian GNU/Linux für alle ..::
    Home | MAME Arcade | Screenshots

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •