I have a very large file ~400G, and I need to remove the last 2 lines from it. I tried to use sed, but it ran for hours before I gave up. Is there a quick way of doing this, or am I stuck with sed?
|
I haven't tried this on a large file to see how fast it is, but it should be fairly quick. To use the script to remove lines from the end of a file:
It seeks to the end of the file, checks to make sure the last character is a newline, then reads each character one at a time going backwards until it's found three newlines and truncates the file just after that point. The change is made in place. Edit: I've added a Python 2.4 version at the bottom. Here is a version for Python 2.5/2.6:
Here's a Python 3 version:
Here is a Python 2.4 version:
|
|||||||||||||
|
|
you can try GNU head
|
|||||||||||||
|
|
The problem with sed is that it is a stream editor -- it will process the entire file even if you only want to make modifications near the end. So no matter what, you are creating a new 400GB file, line by line. Any editor that operates on the whole file will probably have this problem. If you know the number of lines, you could use You might have better luck using |
||||
|
|
|
I see my Debian Squeeze/testing systems (but not Lenny/stable) include a "truncate" command as part of the "coreutils" package. With it you could simply do something like
to remove 160 bytes from the end of the file (obviously you need to figure out exactly how many characters you need to remove). |
|||||
|
|
Try VIM...I'm not sure if it will do the trick or not, as I've never used it on such a big file, but I've used it on smaller larger files in the past give it try. |
|||
|
What kind of file and in what format? May be easier to use something like Perl dependent on what kind of file it is - text, graphics, binary? How is it formatted - CSV, TSV... |
|||||||
|
|
If you know the size of the file to the byte (400000000160 say) and you know that you need to remove exactly 160 characters to strip the last two lines, then something like
should do the trick. It's been ages since I used dd in anger though; I seem to remember things go faster if you use a bigger block size, but whether you can do that depends on whether the lines you want to drop are at a nice multiple. dd has some other options to pad text records out to a fixed size which might be useful as a preliminary pass. |
|||
|
|
If "truncate" command isn't available on your system (see my other answer), look at the "man 2 truncate" for the system call to truncate a file to a specified length. Obviously you need to know how many characters you need to truncate the file to (size minus the length of the problem two lines; don't forget to count any cr/lf characters). And make a backup of the file before you try this! |
||||
|
|
#!/bin/sh ed "$1" << HERE $ d d w HERE changes are made in place. This is simpler and more efficient than the python script. |
||||
|
|
Modified the accepted answer to solve a similar problem. Could be tweaked a little bit to remove n lines.
And the corresponding test:
|
|||
|
|
head -n -2 file– user31894 Apr 6 '10 at 5:54