Differences between revisions 5 and 6
Revision 5 as of 2009-01-09 14:09:01
Size: 651
Editor: 212
Comment:
Revision 6 as of 2009-01-11 09:31:26
Size: 590
Editor: ppp089210047077
Comment: ed sucks
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
ed -s file << EOF
1i
ex file << EOF
0a
Line 15: Line 15:
Please note that this will not work if the file is empty. ed will also add a newline character to the end of the file if it's missing. ex will also add a newline character to the end of the file if it's missing.

How do I prepend a text to a file (the opposite of >>)?

You cannot do it with bash redirections alone; the opposite of >> does not exist....

To insert content at the beginning of a file, you can use an editor:

ex file << EOF
0a
header line 1
header line 2
.
w
EOF

ex will also add a newline character to the end of the file if it's missing.

Or you can rewrite the file, using things like:

{ echo line; cat file ;} >tmpfile && mv tmpfile file
echo line | cat - file > tmpfile && mv tmpfile file

or lots of other solutions.

BashFAQ/090 (last edited 2017-02-14 19:48:12 by GreyCat)