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: Need a Custom or Prewritten Perl Program?: I need a program that...:
Need a Program that changes content of a file

 



gururaj
New User

Mar 13, 2009, 9:25 PM

Post #1 of 6 (2835 views)
Need a Program that changes content of a file Can't Post

Hi...

I am new to perl.I need a program which changes the content of a file
The content of the file is

msgauth=5
msgusr=8
usrpass=10

I need to change all values from 5,8 and 10 to 3.Kinldy help me on the same.


gnix
Novice

Mar 23, 2009, 1:56 AM

Post #2 of 6 (2801 views)
Re: [gururaj] Need a Program that changes content of a file [In reply to] Can't Post


Code
#!/usr/bin/perl -w 

$filename = "text.txt";

open(IN, "<$filename") || die "Can't open $filename";
open(OUT,">new_$filename") || die "Can't open new_$filename";

while(<IN>) {
s/^([a-z]+)=\d+$/$1=3/g;
print OUT $_;
}

close(IN);
close(OUT);


gnix


KevinR
Veteran


Mar 23, 2009, 1:28 PM

Post #3 of 6 (2791 views)
Re: [gnix] Need a Program that changes content of a file [In reply to] Can't Post

You might want to avoid doing students work for them.
-------------------------------------------------


vikas.deep
User

May 3, 2009, 3:50 AM

Post #4 of 6 (2639 views)
Re: [gnix] Need a Program that changes content of a file [In reply to] Can't Post

Hello gnix,
You have already answered the query but I wondered if the person who had originally posted the query checked the solution proposed by you.
In fact I checked it and I found out that it needs a minor improvement You see while<IN> and then the substitution works well for the first two lines but fails in the last input line because the last input line is also carrying a white space character (You check it yourself;only the first two lines are changed) so I had modified the code a bit and my solution is as follows
#!/usr/bin/perl -w
$filename = "text.txt";
open(IN, "<$filename") || die "Can't open $filename";
open(OUT,">new_$filename") || die "Can't open new_$filename";
while(<IN>) {
s/^([a-z]+)=\d{1,2}\s*$/$1=3/g;
print OUT $_,"\n";
}
close(IN);
close(OUT);
Now it will also change the last line (i.e usrpass=10) previously it was changing only the first two lines. As far as digits are concerned I used \d{1,2} because all the three lines of input had either one or two digits only.

Quote
Criticism is very welcome because it improves meSmile
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

(This post was edited by vikas.deep on May 3, 2009, 3:51 AM)


KevinR
Veteran


May 3, 2009, 10:13 AM

Post #5 of 6 (2623 views)
Re: [vikas.deep] Need a Program that changes content of a file [In reply to] Can't Post

The space was probably just a copy and paste artifact.
-------------------------------------------------


vikas.deep
User

May 4, 2009, 12:54 AM

Post #6 of 6 (2613 views)
Re: [KevinR] Need a Program that changes content of a file [In reply to] Can't Post

After the veteran's comment I re-tried it but this time using my mouse I carefully placed the cursor next to the last character a zero in this case and pressed the "delete-key" on my keyboard for a few seconds and now the original solution proposed was working without a \s* required.
I understand that it is always advisable to repeat this feet (press the del key few times or keep it pressed for a few seconds after the last character in your input file) when working on files otherwise this "copy-paste artifact" may creep in or use \s* in the regex capture and/or s//. Am I rightUnimpressed
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


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

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