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: Re: [gerble1000] how to rewrite a specific line of a txt file: Edit Log



Laurent_R
Veteran / Moderator

Aug 27, 2012, 11:46 AM


Views: 2021
Re: [gerble1000] how to rewrite a specific line of a txt file

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)


Edit Log:
Post edited by Laurent_R (Veteran) on Aug 27, 2012, 11:48 AM


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

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