want to prepend to a text file, not append
I found this quite an interesting thought.
">>" if of course the append operator, it will stick STDIN onto a file of your choice, so it gets added on the end. Great! Everybody finds that useful. But how about prepending? I mean, sending some STDIN to the beginning of a textfile. There seems to be a "forward" view to memory. It's OK to add and append stuff, but going backward, seems less conventional. So, I thought about dong a little C program for this. But maybe this han been come across already. On first glance it may appear easy, but it may turn out in practice not to be. Ways: One: Code:
echo "new first line" >newpendfile.txt && cat oldfile.txt >> newpendfile.txt && rm -f oldfile.txt |
append to tac'd version of the file. Then re-tac while overwriting file.
Nope. too messy. |
I'm sure there are probably a dozen ways to do what you want. I know sed can insert lines at the beginning of the text, for example. But even just your example above can be done more cleanly.
Code:
echo -e "new first line\n$(cat file.txt)" >file.txt |
Hi David,
Many thanks, that's actually quite a good one. (yes, works really well .thanks!) I've also decided I want the worst possible and the best possible way for this. Definitely your suggestion helps in the latter. For the former, I open the text file in Openoffice, and type the "newfirstline" and then go through a bunch of dialogs in order to save as text. A good 5 minutes is required for this, and it is not automatable. No, I write the first line on paper, I scan and perform OCR, which is then echoed out with a subshell (as David) cat'ng out the file and overwriting. Good if tedious 20 minutes. |
You mentioned sed ...
Code:
sed -e ' |
Hi joeBuffer, thanks for the contribution.
I'm also following two tracks ... 1) fooling >> into prepending rather than appending 2) fseeking to a memory location earler than the file itself and inserting the firt line toegther with \n in there (if kernel will let me). |
Quote:
OpenOffice has API and, I think, Python interface, maybe bindings. |
Hi Sergei,
OK, I retract. I am being facetious, you know ... joking. Nearly everything is automatable in computing, but sometimes the effort required outweighs the benefits, a fact the theory often misses out on. cheers. |
All times are GMT -5. The time now is 08:45 AM. |