LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   want to prepend to a text file, not append (https://www.linuxquestions.org/questions/programming-9/want-to-prepend-to-a-text-file-not-append-744568/)

urentity 08-02-2009 01:26 PM

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
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.

urentity 08-02-2009 02:07 PM

append to tac'd version of the file. Then re-tac while overwriting file.

Nope. too messy.

David the H. 08-02-2009 02:07 PM

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

urentity 08-02-2009 02:15 PM

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.

joeBuffer 08-02-2009 03:08 PM

You mentioned sed ...
Code:

sed -e '
1i\
new first line' oldfile.txt > newpendfile.txt && rm oldfile.txt


urentity 08-02-2009 03:46 PM

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).

Sergei Steshenko 08-02-2009 03:48 PM

Quote:

Originally Posted by urentity (Post 3628684)
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.

???

OpenOffice has API and, I think, Python interface, maybe bindings.

urentity 08-03-2009 12:45 AM

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.