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:
substituting in a file

 



nibalan
Novice

Jun 8, 2011, 7:27 PM

Post #1 of 5 (479 views)
substituting in a file Can't Post

I have a file manual.pl with the following contents

@value_0="version to be used ";
@value_1="./hi/hello.c /view/nibalan-branch/vob/ios/sys/if gcc-c -MD -MF compiler.d -nsdiop sddkl -atomrse /be/bfbg/dgg
dfff /fg/dfd/dfdsf/atom.c/fine/ gcc -c atom depend any one
t-pointer-x -Wno-parentheses ../atom/atom_c7100.c";
1;


When I execute the following code:
$login=getlogin();
open(MANUALANS, "+</users/$login/manual.pl");
@file = <MANUALANS>;
foreach $file (@file){
$file =~ s/\/view\/nibalan-branch\/vob/\/vob/g;
print MANUALANS $file;
}
close MANUALANS;

I get the following output in manual.pl

@value_0="version to be used ";
@value_1="./hi/hello.c /vob/ios/sys/if gcc-c -MD -MF compiler.d -nsdiop sddkl -atomrse /be/bfbg/dgg dfff /fg/dfd/dfdsf/atom.c/fine/ gcc -c atom depend any one t-pointer-x -Wno-parentheses ../atom/atom_c7100.c";
1;
eses ../atom/atom_c7100.c";
1


Can you please help me out?


BillKSmith
Veteran

Jun 8, 2011, 8:39 PM

Post #2 of 5 (472 views)
Re: [nibalan] substituting in a file [In reply to] Can't Post

What exactly did you expect to happen? You overwrote your file with a shorter version. The rest of the file remains as it was.



It is not really possible to edit a text fie in place. Refer to the -i and -p options in perldoc perlrun for one solution.
Good Luck,
Bill


nibalan
Novice

Jun 8, 2011, 10:46 PM

Post #3 of 5 (470 views)
Re: [BillKSmith] substituting in a file [In reply to] Can't Post

I want to replace /view/nibalan-branch/vob/ with /vob/ in the file. Is it not possible?


miller
User

Jun 9, 2011, 1:05 AM

Post #4 of 5 (464 views)
Re: [nibalan] substituting in a file [In reply to] Can't Post

Just open and close the file twice, once in read mode and once in write mode. You can use read/write mode, but it would require you to seek and truncate. Not to mention flocking the file.


Code
use strict; 
use warnings;

my $login = getlogin();

my $file = "/users/$login/manual.pl";
open my $fh, $file or die "Can't open $file: $!";
my @file = <$fh>;
close $fh;

s{/view/nibalan-branch/vob}{/vob}g for (@file);

open $fh, '>', $file or die "Can't open $file: $!";
print $fh @file;
close $fh;


If you want to do more, just look at http://perldoc.perl.org/perlfaq5.html. The section "I don't get locking...".

- Miller


BillKSmith
Veteran

Jun 9, 2011, 4:31 AM

Post #5 of 5 (461 views)
Re: [nibalan] substituting in a file [In reply to] Can't Post

Did you read my reference????


Code
#!perl -pibak 
s/\/view\/nibalan-branch\/vob/\/vob/g;



Run this file with the name of the file you want to edit as an argument.
Good Luck,
Bill

 
 


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

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