A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Languages
»
Perl
Author
Replacing text in a file using perl script
Patricia Samuel
Ranch Hand
Joined: Sep 12, 2007
Posts: 300
posted
Aug 06, 2010 10:38:56
0
Hi All,
I need to replace some value in config file using perl script. I used this code -
open (IN, "default.properties"); @file = <IN>; seek IN,0,0; foreach $file (@file){ $file =~ s/ftp='pass'/ftp='pass1'/g; print IN $file; } close IN;
When i print $file , it shows me the changed rows but it does not write it to a file. Please suggest.
Regards,
Patricia samuel
James Sabre
Ranch Hand
Joined: Sep 07, 2004
Posts: 781
I like...
posted
Aug 07, 2010 06:23:16
0
Since your are reading the whole file into memory you can safely re-open the file and write to it i.e.
#!/usr/bin/perl $file = "default.properties"; open (IN, $file) || die "Cannot open file ".$file." for read"; @lines=<IN>; close IN; open (OUT, ">", $file) || die "Cannot open file ".$file." for write"; foreach $line (@lines) { $line =~ s/hello/hello hello hello/ig; print OUT $line; } close OUT;
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: Replacing text in a file using perl script
Similar Threads
unix2dos command in perl script
Naming policy
Writing a file and executing
Running perl from java
servlet calling perl script - script hangs
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter