Re: Inserting at begining of file.

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 10/25/04


Date: 25 Oct 2004 11:34:08 GMT

2004-10-25, 03:39(-07), Gopu Bhaskar:
> Is it possible, through shell commands, to insert text at the
> _begining_ of a file (without using a temporary file) ?.
> Thanks in advance,

You can do it as:

{
  rm file.txt
  echo text to insert
  cat
} < file.txt

which may reveal dangerous.

Or you can use ex:

ex -s file.txt << \EOF
1i
text to insert
.
x
EOF

(ex may use tempfiles internally).

With zsh internal temp files:

echo text to insert | cat - =(cat file.txt) 1<> file.txt

Actually only the length of your inserted string needs only to
be saved in memory, so you could use a short perl script that
opens the file read-write, reads initial n-bytes, then write the
text to insert (n bytes long) and then a loop that reads n bytes
and writes the previous n bytes to be written.