L
Li Chen
Hi all,
I want to search one pattern in a file line by line and replace it with
new pattern one. But it doesn't work. I cannot change the old pattern
into the new one using gsub! Is there any problem with my script?
Thanks,
Li
################Ruby script##############
#create a regular expression and its replacement
r1_obj=Regexp.escape('1.000000\$P1G\1.000000') #pattern
r2_obj=Regexp.escape('2.000000\$P1G\2.000000') #replacement
DATA.each_line do |line|
if line.match(r1_obj)
line.gsub!(r1_obj, r2_obj) #
puts line
end
end
__END__
test1.001
K:\flow\test\test1.001
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
test1.002
K:\flow\test\test1.002
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
test1.003
K:\flow\test\test1.003
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
####screen output###########
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
I want to search one pattern in a file line by line and replace it with
new pattern one. But it doesn't work. I cannot change the old pattern
into the new one using gsub! Is there any problem with my script?
Thanks,
Li
################Ruby script##############
#create a regular expression and its replacement
r1_obj=Regexp.escape('1.000000\$P1G\1.000000') #pattern
r2_obj=Regexp.escape('2.000000\$P1G\2.000000') #replacement
DATA.each_line do |line|
if line.match(r1_obj)
line.gsub!(r1_obj, r2_obj) #
puts line
end
end
__END__
test1.001
K:\flow\test\test1.001
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
test1.002
K:\flow\test\test1.002
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
test1.003
K:\flow\test\test1.003
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
####screen output###########
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx
xxx\1.000000\$P1G\1.000000\xxx
yyyy\1.000000\$P1G\1.000000\xxx