Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prepend to text file 2

Status
Not open for further replies.

shrubble

MIS
Jul 23, 2003
300
US
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?

Thanks!


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

-Shrubble
 
This is one way ( I like the File::Slurp module ):
Code:
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 );

--jim
 
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.
 
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.
 
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:
Code:
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 );

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

-Shrubble
 
stevexff said:
...you 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 :)

Mike

"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

 
wouldn't this sort of thing work?

Code:
$filename = "textfile.txt";

$contents = `cat $filename`;

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


Kind Regards
Duncan
 
or how about this?

put this into a file saved as prepend

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

then run this from the terminal:-

[blue]perl -i.old -p prepend textfile.txt[/blue]

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

i.e. if the file currently contains:-

[red]existing text...[/red]

it will end up containing:-

[red]added afterwards with Perl -i in-place editing
existing text...[/red]



Kind Regards
Duncan
 
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

-Shrubble
 
Duncan, I think you're cheating a bit with that first one.
You can do the whole thing with system commands.
Code:
`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?
 
I completely agree with you Mike :)

... I was just exploring different possibilities


Kind Regards
Duncan
 
But it doesn't work when I run the perl script from Web.
What is the crux?? Thanks for help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top