Thanks. We have received your request and will respond promptly.
Log In
Come Join Us!Are you aComputer / IT professional?Join Tek-Tips Forums!Talk With Other MembersBe Notified Of ResponsesTo Your PostsKeyword SearchOne-Click Access To YourFavorite ForumsAutomated SignaturesOn Your PostsBest Of All, It's Free!Join Us!*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.Posting GuidelinesPromoting, selling, recruiting, coursework and thesis posting is forbidden. Students Click HereTek-Tips Posting PoliciesContact US read a line from a file and writing to the same line in the filethread219-1428684 ForumSearchFAQsLinksMVPsForumSearchFAQsLinksMVPs read a line from a file and writing to the same line in the file read a line from a file and writing to the same line in the file bcdixit (TechnicalUser)(OP)20 Nov 07 14:25How do I write to the same line in a file after reading from the that line.#!/usr/contrib/bin/perl$i = "c.txt";$flag = 0;if (-e $i ){ open(RW,"+", $i); print RW "0"; $flag = 1; }if($flag == 0){ while() { $tmp = $_; } $tmp=$tmp+1; print RW $tmp;} close RW;I want this program to create a file called c.txt if it doesnot exist and on the first run of the perl script it should write a '0' to first line in the file. the next time I run the script, it should read the first line(i.e. a 0), increment it by 1 and then re-write to the same line. i.e. the first line on the file should be written with '1'. Currently when i run the script for the first time, it correctly write a '0' on the first line but when I rerun the script it appends the incremented value i.e. a '1' (writes a '01') on the first line. I expect it to write a '1' on first line. Any help is appreciated. Thanks RE: read a line from a file and writing to the same line in the file travs69 (MIS)20 Nov 07 14:40use Tie::File ??~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
RE: read a line from a file and writing to the same line in the file ishnid (Programmer)20 Nov 07 14:52Absolutely. Tie::File would be the way to go. RE: read a line from a file and writing to the same line in the file MillerH (Programmer)20 Nov 07 15:44perlfaq5 - How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?- Miller RE: read a line from a file and writing to the same line in the file bcdixit (TechnicalUser)(OP)20 Nov 07 16:48is there another way of doing this...I am getting this ...Can't locate TIE/File.pm in @INC (@INC contains: /opt/perl5/lib/5.6.1/PA-RISC2.0 /opt/perl5/lib/5.6.1 /opt/perl5/lib/site_perl/5.6.1/PA-RISC2.0 /opt/perl5/lib/site_perl/5.6.1 /opt/perl5/lib/site_perl .) RE: read a line from a file and writing to the same line in the file ishnid (Programmer)20 Nov 07 16:50It's case-sensitive. Tie::File, not TIE::File RE: read a line from a file and writing to the same line in the file bcdixit (TechnicalUser)(OP)20 Nov 07 17:02still the same error after ishnids comments. I don't think this module is availalle on the box....and its a pain in the axx to get the sysadmin to compile it. RE: read a line from a file and writing to the same line in the file ishnid (Programmer)20 Nov 07 18:03It's a pure Perl module so it doesn't need to be compiled. Have a look here for more on installing modules when you don't have root access. RE: read a line from a file and writing to the same line in the file KevinADC (TechnicalUser)20 Nov 07 19:17Very old version of perl, 5.6.1, might not have Tie::File. You can use perls inplace editor which is what Millers link will discuss. It's much more efficient than Tie::File anyway from what I have onserved.------------------------------------------- Kevin, perl coder unexceptional!
RE: read a line from a file and writing to the same line in the file KevinADC (TechnicalUser)20 Nov 07 19:18onserved = observed------------------------------------------- Kevin, perl coder unexceptional!
RE: read a line from a file and writing to the same line in the file bcdixit (TechnicalUser)(OP)20 Nov 07 19:37I checked Millers Link.. sorry but can you help me understand what is the "inplace editor"? RE: read a line from a file and writing to the same line in the file ishnid (Programmer)20 Nov 07 21:28Miller's link actually recommends Tie::File, as it's standard from 5.8.0.Installing the Tie::File module only involves creating one directory, copying one file and adding one line to the top of your script.1) Create a directory called (for example) /home/username/modules/Tie2) Grab the source code from here and save it in a file called (following the example above) /home/username/modules/Tie/File.pm3) Then in your script, before you load the module with "use Tie::File;", add the following line:CODEuse lib '/home/username/modules'; RE: read a line from a file and writing to the same line in the file KevinADC (TechnicalUser)20 Nov 07 23:21using perl inplace editor:CODE#!/usr/contrib/bin/perluse strict;use warnings;my $i = "c:/perl_test/c.txt";if ( -e $i ){ local @ARGV = ($i); local $^I = '.bak'; while ( ) { print ++$_; }}else { open(RW, ">", $i) or die "$!"; print RW "0";}------------------------------------------------------------Pragmas (perl 5.8.8) used :strict - Perl pragma to restrict unsafe constructswarnings - Perl pragma to control optional warnings------------------------------------------- Kevin, perl coder unexceptional!
RE: read a line from a file and writing to the same line in the file KevinADC (TechnicalUser)20 Nov 07 23:23this part:CODEelse { open(RW, ">", $i) or die "$!"; print RW "0";}better written as:CODEelse { open(RW, ">", $i) or die "$!"; print RW "0"; close RW; }------------------------------------------- Kevin, perl coder unexceptional!
Red Flag SubmittedThank you for helping keep Tek-Tips Forums free from inappropriate posts.The Tek-Tips staff will check this out and take appropriate action.
Reply To This ThreadPosting in the Tek-Tips forums is a member-only feature.Click Here to join Tek-Tips and talk with other members! Already a Member? Login
Join | Advertise
Copyright © 2024 WTWH Media LLC. All Rights Reserved.The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media Privacy Policy
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:
Talk To Other MembersNotification Of Responses To QuestionsFavorite Forums One Click AccessKeyword Search Of All Posts, And More...Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close