perlwannabe wrote:
> Now that I have, with all of you helping, dealt with the unlink problem.
> I am now faced with a new problem.
> 
> I have a file, myfile.txt, that consists of various text items that are
> repetitive.  I am trying to replace many of the repetitive items with a
> place holder. Here is an example of myfile.txt:
> 
> This is a long file with sentences, paragraphs and returns.  Certain
> portions of the file never change and are simply repetitive. I would like
> to replace all of the repetitive items with a placeholder...call it
placey."
> _END FILE_
> 
> I would like to replace "This ... items" with "placey" so the above would
> look like:
> 
> placey with a placeholder...call it "placey."
> 
> When attacking this problem I thought it would be best to use hex values
> so as not to deal with returns, etc.  myfile.txt in hex is:
> 
> 54 68 69 73 20 69 73 20 61 20 6C 6F 6E 67 20 66 69 6C 65 20 77 69 74 68 20
> 73 65 6E 74 65 6E 63 65 73 2C 20 70 61 72 61 67 72 61 70 68 73 20 61 6E 64
> 20 72 65 74 75 72 6E 73 2E 20 20 43 65 72 74 61 69 6E 20 70 6F 72 74 69 6F
> 6E 73 0D 0A 6F 66 20 74 68 65 20 66 69 6C 65 20 6E 65 76 65 72 20 63 68 61
> 6E 67 65 20 61 6E 64 20 61 72 65 20 73 69 6D 70 6C 79 20 72 65 70 65 74 69
> 74 69 76 65 2E 20 20 49 20 77 6F 75 6C 64 20 6C 69 6B 65 20 74 6F 20 72 65
> 70 6C 61 63 65 20 61 6C 6C 0D 0A 6F 66 20 74 68 65 20 72 65 70 65 74 69 74
> 69 76 65 20 69 74 65 6D 73 20 77 69 74 68 20 61 20 70 6C 61 63 65 68 6F 6C
> 64 65 72 2E 2E 2E 63 61 6C 6C 20 69 74 20 22 70 6C 61 63 65 79 2E 22
> 
> Here is the little script that I am working with and, obviously, is not
> working:
> 
> my $filefirst = "c:/perl/myfile.txt";
> open(FILE,"<$filefirst") || die "Could not open file for reading!  $!";
> open(TEMP,">$filefirst.tmp") || die "Could not open file for writing!
$!";
> # I have used the . . . to show that the entire hex string is really there
> my $hextostr = '\x54\x68\x69\x73 . . . \x79\x2E\x22'; 
> while(<FILE>){
>    $_ =~ s/$hextostr/placey/gi;
>    print TEMP $_;
> }

You don't need the hex business (although it will work). Assuming your
long literal is in a variable $string, just:

   s/\Q$string/placey/gi;

will work (do you need /i?)

The problem appears to be that you are reading the file line by line,
but want to search and replace over multiple lines. If you set $/ to
undef, you can read the whole file into a single string and perform
the replacement that way.

> 
> 
> Perhaps I am attacking this problem wrong by using hex, but it seems
> easier to use then text which has carriage returns, tabs, and spaces.

How so?

   $string = "Here is text\twith\n\nnewlines and tabs\tand spaces.\n";

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to