help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Prepending text to the beginning of a file


From: Greg Wooledge
Subject: Re: Prepending text to the beginning of a file
Date: Fri, 17 Jun 2022 07:27:11 -0400

On Fri, Jun 17, 2022 at 08:14:27AM +0300, Tapani Tarvainen wrote:
> Yes. There's no general, safe way to do it with shell builtins.
> 
> If the file is small and its contents are known well enough, various
> hacks are possible but not really worthwhile, or safe.
> 
> Editors are not quite safe either if the files can contain arbitrary
> bytes including nulls, and most of them either use a temporary file
> internally or read the entire file in memory anyway (or both).
> 
> If it really needs to be done without a temporary file and without
> reading the entire file in memory, e.g., if the file is really huge,
> it *can* be done using dd, but it's not exactly trivial: multiple dd
> calls are generally needed and calculating the moves is difficult and
> error-prone.  Making it interruption-safe is harder still.

Most importantly of all, that kind of byte-offset-based random access
writing to a file requires that the file be of a binary type (not text),
with some kind of known format.  The bytes written must be one-to-one
replacements for existing bytes.

What you *cannot* do in that situation is "prepend text to the beginning
of a file".  Any kind of insertion (which "prepending" is) or deletion
would require all of the existing bytes beyond the change point to be
moved to different locations in the file.

Moreover, "text" files don't have a byte-offset-based layout.  Each "line"
is of an arbitrary, usually varying, length.  Anything that changes
the number of lines, or changes the length of *any* existing line, will
once again require moving all of the existing content beyond the change
point to new locations within the file.

Therefore, the only sensible thing to do when you're "editing" a text
file is to write out a brand new version of it into a new file, and
then move that over the original once the writing is done.

(I'm sure Tapani knows this, but I'm reiterating it for the OP, and for
anyone else reading this.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]