[Linux-disciples] Deleting the first N lines of a file

Chung-chieh Shan ccshan at post.harvard.edu
Thu Feb 9 00:23:10 EST 2006


On 2006-02-08T20:55:49-0500, Stephen R Laniel wrote:
> Does anyone know of a very fast way to delete the first N
> lines of a file? (That is, after the deletion, the file's
> size will reduce from N + k to k lines.) Speed is rather
> important here, so something like
> 
> head -n -N [filename] > end.txt
> mv end.txt [filename]
> 
> won't do. [filename] may be very large -- on the order of
> 1GB.

You can do it in-place.  You can either use open(), read(), write(),
close(), and truncate(), or use open(), mmap(), memcpy(), munmap(),
close(), and truncate().  If I recall correctly, the first way is
preferred for single-processor systems, and the second way is preferred
for multi-processor systems.  Both ways can be implemented in C (don't
use buffered IO).  The first way can be implemented in Perl (don't use
buffered IO), and I don't think the second way can be implemented in
Perl, but one can always be wrong to assert that something cannot be
implemented in Perl.

-- 
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
http://bash.org/?454203
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20060209/ee779e63/attachment.pgp


More information about the Linux-disciples mailing list