Re: [SLUG] Re: Insert text at the beginning of a file

2004-12-15 Thread Daniel Bush
Dan Treacy wrote:
[EMAIL PROTECTED] wrote:
Ouch
a problem begging for perl as a solution!
James

Probably, but seeing as though my shell skills are half a step above 
totally crap and my perl skills are 4 steps below shell it is :-)

And thanks everyone for your suggestions I haven't had a chance to 
test any of them out. maybe tonight.

Dan.
If you're talking about inserting text at the beginning of a file, than 
perl has a tidy way of doing it all for you:

$ perl -pi -e "s/^/foobar\n/ if ($.==1);" test.file
That's pretty easy!
See the 'perlrun' manpage.
Doesn't seem to work if test.file is empty ie 0 lines.
Nice variation where you can say '-i.orig' and you will have the 
original stored with a '.orig' extension, or whatever you want.
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] Re: Insert text at the beginning of a file

2004-12-15 Thread Dan Treacy
[EMAIL PROTECTED] wrote:
Ouch
a problem begging for perl as a solution!
James

Probably, but seeing as though my shell skills are half a step above 
totally crap and my perl skills are 4 steps below shell it is :-)

And thanks everyone for your suggestions I haven't had a chance to test 
any of them out. maybe tonight.

Dan.
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


[SLUG] Re: Insert text at the beginning of a file

2004-12-15 Thread jam
Ouch

a problem begging for perl as a solution!
James

> On Wed, 2004-12-15 at 03:01 +1100, Dan Treacy wrote:
> > I have a text file that I'm trying to update from a shell script but the 
> > information needs to be inserted at the beginning of the file.
> > 
> > I have a couple of ways to do this that involves lots of "cat"ing and 
> > mving and temporary files and the like etc. Not real high on the 
> > efficiency scale.
> 
> By the time you muck around it is far easier to do something like this:
> 
> echo SOMETEXT >newfile$$
> echo "some more text and $VARIABLE" >> newfile$$
> cat oldfile >> newfile$$
> mv oldfile oldfile.bak
> mv newfile$$ oldfile
> 
> If you consider what is happening with the filesystem anyway either you
> do it or the OS does it so the result is the same.
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html