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...: Re: [gnix] Need a Program that changes content of a file: Edit Log



vikas.deep
User

May 3, 2009, 3:50 AM


Views: 2734
Re: [gnix] Need a Program that changes content of a file

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)


Edit Log:
Post edited by vikas.deep (User) on May 3, 2009, 3:51 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