LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-02-2009, 02:26 PM   #1
urentity
Member
 
Registered: Jan 2009
Posts: 48

Rep: Reputation: 15
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.

Last edited by urentity; 08-02-2009 at 02:34 PM.
 
Old 08-02-2009, 03:07 PM   #2
urentity
Member
 
Registered: Jan 2009
Posts: 48

Original Poster
Rep: Reputation: 15
append to tac'd version of the file. Then re-tac while overwriting file.

Nope. too messy.
 
Old 08-02-2009, 03:07 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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
 
Old 08-02-2009, 03:15 PM   #4
urentity
Member
 
Registered: Jan 2009
Posts: 48

Original Poster
Rep: Reputation: 15
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.

Last edited by urentity; 08-02-2009 at 03:22 PM.
 
Old 08-02-2009, 04:08 PM   #5
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
You mentioned sed ...
Code:
sed -e '
1i\
new first line' oldfile.txt > newpendfile.txt && rm oldfile.txt

Last edited by joeBuffer; 08-02-2009 at 04:55 PM.
 
Old 08-02-2009, 04:46 PM   #6
urentity
Member
 
Registered: Jan 2009
Posts: 48

Original Poster
Rep: Reputation: 15
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).
 
Old 08-02-2009, 04:48 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by urentity View Post
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.
 
Old 08-03-2009, 01:45 AM   #8
urentity
Member
 
Registered: Jan 2009
Posts: 48

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Java] How to append something to each line in a text file ? Kunsheng Programming 3 04-15-2009 10:08 PM
append a line to text file in script roadrash Linux - Newbie 4 10-01-2008 10:03 AM
How can I append text to a string in a file HGeneAnthony Linux - Newbie 4 03-01-2007 01:16 PM
quickly append text to file blackzone Linux - General 3 09-07-2006 06:48 AM
pipe output to append to a text file davee Linux - Newbie 3 08-27-2003 06:03 AM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration