regular expression (.*?)

hi all,
i have a text file with below content

...............................
...............................
...............................
...............................
%%Page: (4) 4
%%PageBoundingBox: 34 -30 584 831
%%BeginPageSetup
%%BeginFeature: *PageSize A4
595 842 SetPageSize
%%EndFeature
%%BeginFeature: *Duplex None
false false SetDuplexMode
%%EndFeature
%%EndPageSetup
GS
.........................
................................
..........................................

i want to add some lines after GS so in perl i tried this

$data = ~s/%%Page: (4) 4(.*?)GS/%%Page: (4) 4 $1 GS setlinewidth \n 25 110 moveto \n 75 110 lineto/g;

where $data holds file content. this is working fine and the output is

...............................
...............................
...............................
...............................
%%Page: (4) 4
%%PageBoundingBox: 34 -30 584 831
%%BeginPageSetup
%%BeginFeature: *PageSize A4
595 842 SetPageSize
%%EndFeature
%%BeginFeature: *Duplex None
false false SetDuplexMode
%%EndFeature
%%EndPageSetup
GS
setlinewidth
25 110 moveto
75 110 lineto
.........................
................................
..........................................

but when i tried this with perl command line it is not working

perl -p -i -e 's/%%Page: (4) 4(.*?)GS/%%Page: (4) 4 $1 GS setlinewidth \n 25 110 moveto \n 75 110 lineto/g' FILENAME

Kindly suggest me the solution(in sed/awk/perl etc)

Thanks in advance

uttam hoode

Something like this?

awk '
$0=="GS"{ 
  printf("%s\n%s\n%s\n%s\n",$0,"setlinewidth","25 110 moveto","75 110 lineto")
  next
}
{print}
' file

Regards

You need to add -0777 to the Perl command line to make it read the whole file in one go, rather than one line at a time. You also need to add an /s modifier to the regex substitution to make dot match a newline (you ought to have needed this in your proof of concept on $data, too).

can u please check this.it didnt work for me

perl -0777 -p -i -e 's/%%Page: (4) 4(.*?)GS/%%Page: (4) 4 $1 GS setlinewidth \n 25 110 moveto \n 75 110 lineto/s' filename

Try this:

perl -i.bck -00 -ple'
s/(%%Page: \(4\) 4.*?GS)/$1\nsetlinewidth \n25 110 moveto \n75 110 lineto/gs
' filename

If you have empty lines between Page: and GS you should use -0777 as era already mentioned.

hi all,
thanks for the reply

all the solutions are working fine(after using -0777).
actully i am calling above commands in perl script and i forgot to escape speacial charactes (% $ \n etc).

#!/usr/bin/perl

......................
.........................

`perl -0777 -p -i -e 's/\%\%Page: \\(4\\) 4\\(\\.\\*\\?\\)GS/\\%\\%Page: \\(4\\) 4 \$1 GS setlinewidth \\n 25 110 moveto \\n 75 110 lineto/s' filename`;

..................
................

Thanks and Regards,
uttam hoode

Um, why are you running Perl in backticks from inside a Perl script ...?

Of course, if it works for you, more power to you etc.

hi era,
PERL script with IO operation was taking around 90 minutes to prcess
1872 postscript with 43000 pages.

now with perl -p -i -e it s taking just 8mins....

perl and regex rocks :b:

Thanks and Regards,
uttam hoode