ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
That's disgusting. IN perl, you could slurp the oldfile.txt and then print out, with the "new first line" coming first. I must admit, I would do the same with the C program. Read in the oldfile.txt first.
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
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.
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).
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.
???
OpenOffice has API and, I think, Python interface, maybe bindings.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.