CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to rewrite a specific line of a txt file

 



gerble1000
User

Aug 27, 2012, 4:14 AM

Post #1 of 18 (1883 views)
how to rewrite a specific line of a txt file Can't Post


Code
 
open(F, '>:utf8', "new.txt");
print F[7] "$myvar";
close F;

something like this,
or am i going the wrong way about this



Laurent_R
Enthusiast / Moderator

Aug 27, 2012, 6:49 AM

Post #2 of 18 (1880 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

You basically cannot rewrite a line in a file (there are some exceptions, but in general, this is true). You need to read the file, write another file with the changes and then rename the files.


Code
open(F, '>:utf8', "new.txt");


This will erathe the full content of the file.


Code
print F[7] "$myvar";


F is a file handler, not an array of lines, so that too would not work.


gerble1000
User

Aug 27, 2012, 6:52 AM

Post #3 of 18 (1879 views)
Re: [Laurent_R] how to rewrite a specific line of a txt file [In reply to] Can't Post

ok.
so do i read the file line by line in a for loop and change the line from there


Laurent_R
Enthusiast / Moderator

Aug 27, 2012, 7:25 AM

Post #4 of 18 (1878 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

Yes, you read your input file line by line in a while loop and write each line read (with modifications) into your output file.


gerble1000
User

Aug 27, 2012, 11:31 AM

Post #5 of 18 (1871 views)
Re: [Laurent_R] how to rewrite a specific line of a txt file [In reply to] Can't Post

could you provide a working example please :)


Laurent_R
Enthusiast / Moderator

Aug 27, 2012, 11:46 AM

Post #6 of 18 (1870 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

Hi,

something like this (quick untested example):


Code
use strict; 
use warnings;

my $infile = "in_file.txt";
my $outfile = "out_file.txt";
open my $in, "<", $infile or "die cannot open $infile $! \n";
open my $out, ">", $outfile or "die cannot open $outfile $! \n";
while (my $line = <$in>){
chomp $line,
# change line if needed. I'll assume for the example you want to replace multiple spaces where they occur by one single space
$line =~ s/\s+/ /g;
print $out $line, "\n";
}
close $in;
close $out;

Code
 
Then, of course, you may want to delete the in_file et rename the out-file into the in_file.


(This post was edited by Laurent_R on Aug 27, 2012, 11:48 AM)


gerble1000
User

Aug 28, 2012, 10:35 AM

Post #7 of 18 (1859 views)
Re: [Laurent_R] how to rewrite a specific line of a txt file [In reply to] Can't Post

how will i know what line i am writing to.
for instance i need to change line 11 to $myvar

am i right in saying the "while" will put the file into $line which i can change via $line[11] = $myvar;
and the print $out $line;


(This post was edited by gerble1000 on Aug 28, 2012, 10:40 AM)


Laurent_R
Enthusiast / Moderator

Aug 28, 2012, 11:07 AM

Post #8 of 18 (1854 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

No, the code I provided reads and process one line at a time. There never any array containing the lines.

You could modify my code to incxrement a counter each time throuhg the while loop.

Or, better, you could use the special variable $. which tells you at which line of the file you are.

So, somewhere in the hile loop you could write:


Code
chomp $line; 
if ($. == 11) {
# change line 11 somehow...
}


Finally, you could also slurp the file into an array, but remember that line 11 will be stored in @array_or_lines[10].


gerble1000
User

Aug 28, 2012, 11:24 AM

Post #9 of 18 (1853 views)
Re: [Laurent_R] how to rewrite a specific line of a txt file [In reply to] Can't Post

would you recomend slurping to an array or is there another method of changing that line in the while loop


gerble1000
User

Aug 28, 2012, 11:47 AM

Post #10 of 18 (1851 views)
Re: [Laurent_R] how to rewrite a specific line of a txt file [In reply to] Can't Post

ok here is what i have


Code
my $file = "1.txt"; 
my $filenew = "2.txt";
my $myvar = "NEW STRING";

open my $in, '<', $file or die "Can't read old file: $!";
open my $out, '>', "$filenew" or die "Can't write new file: $!";


while( <$in> ) # print the lines before the change
{

last if $. == 4; # line number before change
print $out $_;
}

my $line = "$myvar\n";

print $out $line;

while( <$in> ) # print the rest of the lines
{
print $out $_;
}
close $in;
close $out;
unlink( "1.txt" );
rename("2.txt", "1.txt")



i modified some code off a forum.
this works but is this the best way


Laurent_R
Enthusiast / Moderator

Aug 28, 2012, 11:51 AM

Post #11 of 18 (1851 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

No, no, the method with $. works perfectly. But I can't give you the code to change the line since I do not know what change you want to do.

If you just want to replace completely the line 11, then you could have something like this:


Code
my $line_11 = "I have a dream, that my four little children will one day live in a nation where they will not be judged by the color of their skin ...\n"; 
while ($line = <FILE>) {
if ($. == 11) {
print OUT $line_11;
else
print OUT $line;
}
}



gerble1000
User

Aug 28, 2012, 12:45 PM

Post #12 of 18 (1850 views)
Re: [Laurent_R] how to rewrite a specific line of a txt file [In reply to] Can't Post

ok here is the result working

Code
my $newstring = "this is my new line to replace the old 1\n";  

my $infile = "1.txt";
my $outfile = "2.txt";
open FILE, "<", $infile or "die cannot open $infile $! \n";
open OUT, ">", $outfile or "die cannot open $outfile $! \n";
while (my $line = <FILE>) {
if ($. == 11) {
print OUT $newstring; }
else {
print OUT $line;
}
}
close FILE;
close OUT;

unlink('1.txt');
rename('2.txt','1.txt');


is there a better way than to unlink then rename files.
could i not oipen the 1.txt into FILE then close then reopen the 1.txt to output results


Laurent_R
Enthusiast / Moderator

Aug 28, 2012, 11:20 PM

Post #13 of 18 (1838 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

In theory, you could load the whole file in memory and then reopen the same file in write mode (thus crushing its content) and write everything to it, but this is very unsafe.


gerble1000
User

Aug 29, 2012, 9:42 AM

Post #14 of 18 (1832 views)
Re: [Laurent_R] how to rewrite a specific line of a txt file [In reply to] Can't Post

ok.
thankyou for helping. that should keep me busy for a few days altering my code to this


Laurent_R
Enthusiast / Moderator

Aug 29, 2012, 10:48 AM

Post #15 of 18 (1831 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

Don't do it, as I said, this is not safe. The code you wrote is the right way to do it.

A couple of suggestions to make your code safer.

Rather than unlinking the old file, you should probably rename it, for example with an extension such as ".old" or ".bak".

Check the return status of closing your files.


gerble1000
User

Aug 29, 2012, 2:58 PM

Post #16 of 18 (1826 views)
Re: [Laurent_R] how to rewrite a specific line of a txt file [In reply to] Can't Post

ok i was going to do it the safe way but could you explain why that way is unsafe and how do i check the return status .


BillKSmith
Veteran

Aug 29, 2012, 8:50 PM

Post #17 of 18 (1810 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

Consider using the commandline options -p and -i. The -i does all the renaming safely. The -p provides the main loop for you. refer: perldoc perlrun.
Good Luck,
Bill


Laurent_R
Enthusiast / Moderator

Aug 30, 2012, 1:41 AM

Post #18 of 18 (1805 views)
Re: [gerble1000] how to rewrite a specific line of a txt file [In reply to] Can't Post

This is not safe because if the program crashes in the middle, then you no longer have your file.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives