449
Comment: add the 1 in 1i
|
681
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
[[Anchor(faq90)]] | <<Anchor(faq90)>> |
Line 3: | Line 3: |
You cannot do it with bash redirections alone; the opposite of `>>` does not exist.... | |
Line 4: | Line 5: |
You cannot do it with bash redirections only, ie the opposite of >> does not exist. | To do it in 2 commands with sed: sed "1iTEXTTOPREPEND" filename > tmp mv tmp filename |
Line 6: | Line 9: |
You can use an editor: | To insert content at the beginning of a file, you can use an editor: |
Line 8: | Line 11: |
ed -s file << EOF 1i |
ex file << EOF 0a |
Line 16: | Line 19: |
you can use things like: | 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: |
Line 22: | Line 27: |
or a gazillion of other solutions | or lots of other solutions. |
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 do it in 2 commands with sed: sed "1iTEXTTOPREPEND" filename > tmp mv tmp filename
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.