search for and replacing/adding characters in a file

donvar
donvar used Ask the Experts™
on
I have a file upload program that uploads two types of ascii files. One with an ascii form feed character on each page and another with no form feed but the word page with 5 spaces before it at the top of each page. What I want to do is everytime that the word page is found with 5 spaces in front of it I want to add the ascii form feed character before the 5 spaces.

I'm also counting how many pages I have by counting the form feed character.

Here is some of the code:

if(open(OUTFILE, ">$Data/$filename")) {
binmode(OUTFILE);
 while (my $bytesread = read($file, my $buffer, 1024)) {
               print OUTFILE $buffer;
              }
              close (OUTFILE);
              push(@file_did_save, $filename);
              $size = (stat("$Data/$filename"))[7];
        $num_pages = 0;
## Count form feeds
        if(open(COUNTPG,"<$Data/$filename")) {
               while (<COUNTPG>) {
                      if (/$^L/) {
                               $num_pages++;
                            }
               }
               close (COUNTPG);
                    }
## end count of form feed
## Insert of Form feed
   if($num_pages == 0){
## what code do I put in here to do the replace?
             }
## end Inserting

$redirect =~ s//&in_filesize=$size&in_no_of_pages=$num_pages/;
          } else {
              push(@did_not_save, $filename);
          }    
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I have no time to test this, but try

s/ {5}Page/\f/g

If that fails, try

s/[ ]{5}Page/\f/g

Note that in each case, a space precedes the {5}

What this says is.... Find five occurrences of a space and replace this with a form feed character. The g at the end means do this globally.

Andy

Author

Commented:
Thanks Andy I'll try it.

Author

Commented:
Thanks Andy I'll try it.

Author

Commented:
Thanks Andy I'll try it.

Author

Commented:
Thanks Andy, your answer put me on the right tract. I just had to changethe order of a few task in order to get my program to work the way I wanted it to.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial