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: Robert E. Griffith
Subject: Re: Prepending text to the beginning of a file
Date: Thu, 16 Jun 2022 17:12:27 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1

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.



reply via email to

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