[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Prepending text to the beginning of a file
From: |
Akbarkhon Variskhanov |
Subject: |
Re: Prepending text to the beginning of a file |
Date: |
Fri, 17 Jun 2022 18:35:25 +0500 |
In my understanding, it should let me, well, read from and write to the
file for in-place editing.
Let's say that I open a file for both reading and writing:
exec 3<> file
Next, I'd like to write to it:
echo "some rather long text" >&3
At this point it's too soon for seeking and I expect this text to be
prepended to the beginning of the file, since I never explicitly stated to
begin reading from it like so:
cat <&3
Now the reading should begin, and if I redirect the output back to it, the
file should look like this:
"some rather long text"\n
"original text"
If I were to reverse the process (read first, then write), the text should
be appended to the end, as usual.
That's why I kind of tried to "cleverly" (yes, we're all looking for the
opposite word) replace `sed -i` with this redirection operator.
Are my expectations wrong? `cat` allows me to easily concatenate whatever
files, but only outputs to stdout or a separate file. Imagine something
like this:
exec 3<>file
cat - <&3 >&3
1. read stdin from terminal
2. read stdin from file
3. write both to the file in the order specified.
That's what I was hoping for with the <> operator. If it cannot do that,
which seems to be the case, well, then it's not quite clear why it even
exists.
On Fri, Jun 17, 2022 at 6:01 PM Chet Ramey <chet.ramey@case.edu> wrote:
> On 6/16/22 4:48 PM, Akbarkhon Variskhanov wrote:
>
> > To be honest, I can't quite grasp the <> operator's purpose.
>
> It opens the named file for reading and writing, no more, no less.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
> ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/
>