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: Tapani Tarvainen
Subject: Re: Prepending text to the beginning of a file
Date: Fri, 17 Jun 2022 08:14:27 +0300

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.

Tapani

On Thu, Jun 16, 2022 at 05:12:27PM -0400, Robert E. Griffith (bobg@junga.com) 
wrote:
> 
> I suspect that the fundamental problem is that its less efficient to to
> shift the contents of a file to make room to prepend more data in the front
> than using a temp file so to my knowledge that shifting algorithm does not
> exist in any standard tool.
> 
> I think that the best you can do is either use a temporary file or, if you
> know the file is small enough, read it entirely into memory and then write
> the new data and then the old back out to the same file.
> 
> --BobG
> 
> On 6/16/22 16:48, Akbarkhon Variskhanov wrote:
> > Yes, there is sed and ed that can do just that.
> > 
> > But I was just wondering if it can be done with the shell's built-ins and
> > maybe `cat`.
> > 
> > We can concatenate files in the order they're given and output that to
> > another file. Working as intended!
> > 
> > But what if some data is stored in a variable and we want to prepend it to
> > the beginning of a file without creating temporary ones? Sort of like a >>
> > but before the buffer.
> > 
> > I tried something like
> > exec 3<>somefile
> > cat - <&3 >&3 <<eof
> > $(var)
> > eof
> > 
> > And it didn't truncate the file, but also overwrote the first couple of
> > lines. To be honest, I can't quite grasp the <> operator's purpose.
> > 
> > Then I stumbled upon this neat hack: https://stackoverflow.com/a/54715861
> > echo "$(echo -n 'hello'; cat filename)" > filename
> > But that only works if the file is small and fits into echo's arg list.
> > 
> > Please, share your ideas and thoughts. I'm very curious to read them. Try
> > not to resort to editors. :)
> > 
> > Thank you.

-- 
Tapani Tarvainen



reply via email to

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