Differences between revisions 6 and 8 (spanning 2 versions)
Revision 6 as of 2009-01-11 09:31:26
Size: 590
Editor: ppp089210047077
Comment: ed sucks
Revision 8 as of 2010-05-26 12:22:44
Size: 749
Editor: GreyCat
Comment: Move that sed crap down to the bottom and fix the markup.
Deletions are marked like this. Additions are marked like this.
Line 23: Line 23:
or lots of other solutions. Some people insist on using the `sed` hammer to pound in all the screws:
{{{
sed "1iTEXTTOPREPEND" filename > tmp &&
mv tmp filename
}}}

There are lots of other solutions as well.

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

Some people insist on using the sed hammer to pound in all the screws:

sed "1iTEXTTOPREPEND" filename > tmp &&
mv tmp filename

There are lots of other solutions as well.

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