And one last correction (sorry, just woke up and haven't had my coffee yet):

#!/usr/bin/php
<?php

// Set up the terms
$terms = array(
  'this' => 'that',
  'red' => 'blue',
  'stop' => 'go',
);

// Break out the find and replace lists
$find = array_keys($terms);
$replace = array_values($terms);

// Get the document contents
$filename = "/usr/local/something.txt";
$filehandle = fopen($filename, "r");
$document = fread($filehandle, filesize($filename));
fclose($filehandle);

// Perform the find and replace
$document = str_replace($find, $replace, $document);

// Update the document
$filehandle = fopen($filename, "w");
fwrite($filehandle, $document);
fclose($filehandle);

?>


On Oct 27, 2011, at 4:31 AM, Roland Küffner wrote:

> Hi, everybody,
> 
> maybe someone has already a solution to this and is willing to share:
> 
> More often than I thought I find myself having to replace a bunch of terms in 
> a text file with new text. Doing it by hand means doing several 
> search-replace-actions one after another. Putting together a Text Factory 
> would be a solution but very often the whole replace task is a single one - 
> no need to repeat the exact searches ever again.
> 
> My idea was to do it with some kind of dictionary file. In it each line would 
> contain a single search replacement pair separated by tabs. Just like:
> 
> old term<tab>new term
> some other random old text<tab>another replacement
> ...
> 
> Both, search and replacement text is often unsystematic, so doing it with 
> regular expressions is no solution. I tried to fumble together a script that 
> reads such a file (from the desktop, maybe), processes it line by line, 
> searching my current front document for each old term replacing it with the 
> according new value.
> But I failed. I'm afraid, my capabilities as scripter do not even classify as 
> "Beginner".
> 
> Does anyone perchance have some code scaffolding or hints on how to do this - 
> I don't mind if it's Applescript, Perl or Python - I suck at all of them :-(
> 
> Regards,
> Roland
> 
> -- 
> You received this message because you are subscribed to the 
> "BBEdit Talk" discussion group on Google Groups.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> <http://groups.google.com/group/bbedit?hl=en>
> If you have a feature request or would like to report a problem, 
> please email "[email protected]" rather than posting to the group.
> Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

-- 
You received this message because you are subscribed to the 
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem, 
please email "[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

Reply via email to