Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Prepend to text fileHelpful Member!(2) 

shrubble (MIS) (OP)
13 May 04 20:17
Is there an operator (like >>) that will prepend when writing to a text file, as opposed to appending or overwriting? If it has to be done with code (like with shift) what is the easiest way to do it?


deletion mistake
no I can't recover that
you didn't save it


Helpful Member!  Coderifous (Programmer)
13 May 04 21:46
This is one way ( I like the File::Slurp module ):


use File::Slurp;

my $file = '/tmp/foo.txt';
my $prepend_this = "this will be at the top\n";

write_file( $file, $prepend_this, (-e $file)?read_file( $file ):undef );

mikevh (Programmer)
14 May 04 0:14
There's no magical shortcut way of doing this, that I'm aware of.  However you slice it, it comes down to:

1. Open the file for input.
2. Read the contents and hold them someplace (an array or possibly another file).
3. Close the file.
4. Re-open the file for output (this erases the previous contents).
5. Write what you want to prepend.
6. Write the previously-saved contents from step 2.

While variations on the above are possible (such as shoving what you want to prepend onto the front of the array from step 2 with unshift or combining the read/write operations as in Codiferous's example above), this is what it boils down to.
stevexff (Programmer)
14 May 04 3:40
Mike is absolutely right. The file is sitting out there on the disk, and you can't write stuff before the beginning of the file. The file system on any OS can't cope with this. A safe way to do it would be to write the prepended stuff to a new file, append the original file, and then close it. When you are happy that this operation has succeeded, delete the old file and rename the new one back to the same name as the old one. Then if you get a power outage while your script is running, you still have the original file. If you are really paranoid, you can rename the old one rather than deleting it.  
Coderifous (Programmer)
14 May 04 6:48
If you are playing w/ File::Slurp, then it's pertinent to mention that you can achieve stevexff's safer process by setting the 'atomic' option:


use File::Slurp;

my $file = '/tmp/foo.txt';
my $prepend_this = "this will be at the top\n";

write_file( $file, {atomic => 1}, $prepend_this, (-e $file)?read_file( $file ):undef );

shrubble (MIS) (OP)
14 May 04 7:23
Thanks everybody!

I read it into an array, closed the file, opened it wrote the new stuff and then printed the array. Works great!

Thanks Again

deletion mistake
no I can't recover that
you didn't save it


MikeLacey (MIS)
14 May 04 8:16

Quote (stevexff): can't write stuff before the beginning of the file. The file system on any OS can't cope with this...

As a complete aside, and not Perl related at all, you can do this on the AS/400. Not with Perl, so completely off-topic


"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

Helpful Member!  duncdude (Programmer)
14 May 04 11:58
wouldn't this sort of thing work?


$filename = "textfile.txt";

$contents = `cat $filename`;

open (OUTFILE, "> $filename");
print OUTFILE "added text\n$contents";
close OUTFILE;

Kind Regards

duncdude (Programmer)
14 May 04 12:08
or how about this?

put this into a file saved as prepend


s/(.*)/added afterwards with Perl -i in-place editing\n$1/;

then run this from the terminal:-

perl -i.old -p prepend textfile.txt

... the contents of the file will be prepended without any opening/closing of the file

i.e. if the file currently contains:-

existing text...

it will end up containing:-

added afterwards with Perl -i in-place editing
existing text...

Kind Regards

shrubble (MIS) (OP)
14 May 04 13:00
You know, for such a bland topic, I would never have expected such an array of responses!

Actually, it seems kind of strange to me that this issue hasn't been addressed (with a special operator) in the perl language, because so many other text processing "things" have been covered.

Thanks again guys

deletion mistake
no I can't recover that
you didn't save it


mikevh (Programmer)
14 May 04 15:37
Duncan, I think you're cheating a bit with that first one.
You can do the whole thing with system commands.


`echo "prepend this" > prepend.txt`;
`cat prepend.txt textfile.txt > newfile.txt`;
`rename newfile.txt textfile.txt`;
but it's not portable and it's barely writing Perl, is it?

As for the second, it may work, but would you seriously ever do it that way?

I believe in TMTOWTDI, but come on ... isn't this just cleverness for its own sake?
uida1154 (Programmer)
14 May 04 16:51
duncdude (Programmer)
15 May 04 6:20
I completely agree with you Mike

... I was just exploring different possibilities

Kind Regards

PaulTEG (TechnicalUser)
15 May 04 7:28
Can't you copy a file onto another?
nmlsupport (TechnicalUser)
19 May 04 3:17
But it doesn't work when I run the perl script from Web.
What is the crux??  Thanks for help.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close