
vikas.deep
User
May 3, 2009, 3:50 AM
Views: 4805
|
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.Criticism is very welcome because it improves me -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)
|