From: Sam Roberts Date: 2006-04-13T07:14:41+09:00 Subject: Re: Simple substitutions On Wed, Apr 12, 2006 at 08:58:17PM +0900, Peter Bailey wrote: > I have a need to parse postscript print files on a daily basis. Some of > them are over 10 mB. One thing I need to find out is the number of pages > in the files. There's a simple one-liner at the top of each .ps file > that states the number of pages. It reads: > %%Pages: xx > It's a line of its own. > > I simply want to find that line in my source file and then write a > corresponding line in my output file: > Number of pages: xx > > I tried the following in IRB, but I get the syntax error shown below. > > File.open("psout.txt", "w") do |output| > File.foreach("test1.ps") do |line| > line.sub!(/\%\%Pages: ([0-9]{1,5})/"Pages: \1"/) Other people commented on the regex > output << line For efficiency, you can do: if(line =~ /%%Pages: (\d+)/) output << $1 break end YMMV, but stopping processing the file as soon as you've found what you want is faster. Sam