4

This question is philosophically similar to a question that's come up time and time again here:

But they (with the exception of the last one) all deal with simple replacements. I have a somewhat large block of code which appears in many files (I wish copy/pasting source code was punishable by law), and I need to replace it.

Is there anything that will help me out here? I want to avoid "sed"ifying the block (as is done in the third similar question above) because that will take the better part of a day. If I have to temporarily install some particular text editor, so be it.

4
  • The input is a large number of PHP files, and the block of code is a rather lengthy mixture of PHP and inline Javascript. I just want to replace it with include('includes/menu.php');, which contains a corrected version, but inlining / escaping the original would be a nightmare Commented May 11, 2012 at 19:23
  • If the block of code in question can be defined with a regex, I think sed is still your best bet. If it can't be defined, but exists in the same place in each file (same line number start and end), you could potentially do this with a fancy tr. If you dont have either, my guess is you're going to have to do it by hand. Commented May 11, 2012 at 19:27
  • @juwiley would it be possible to define a regex that basically says "look for the string 'general_help', and match the block from <?php to ?> which contains it? I'm not very good with regex :-\ Commented May 11, 2012 at 19:31
  • A note about sed, it seems it is very hard to get it to play nicely with multi-line inputs Commented May 11, 2012 at 21:12

2 Answers 2

5

The nice thing about your question is that you're dealing with fixed strings rather than regular expressions...which makes the problem relatively simple. You could write something to do the job for you without too many problems.

The solution I've linked to isn't optimal, but it does work, and it doesn't rely on matching only opening/closing lines (that is, it only replaces verbatim matches). The basic idea is:

  • Read in the file that defines what we're looking for,
  • Read in the file that defines our replacement,
  • Iterate over all the other arguments, looking for the search string and replacing it with the replacement text
Sign up to request clarification or add additional context in comments.

3 Comments

this looks pretty awesome, testing it out now
gh! none of the indentation matches between copies. I hate being the guy they get to "fix" outsourced code :( still, this is probably my best bet in general so marking as accepted
Doesn't sound like fun. Good luck!
1

If the opening and closing lines are unique you can delete the block and replace it with your code using with sed using:

placeholder=somereallyuniquestringthatwillneverbematched for i in *.php; do # Remove original block using first and last line matching and add placeholder sed -i -e "/The text of the closing line/a\ $placeholder /The Text of the opening Line/,/The Text Of The Closing Line/d" "$i" # Replace placeholder with desired code sed -i -e "s/$placeholder/#include ('include/menu.php');/" "$i" done 

This will only find one occurance of the block per file. As always, make a backup first.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.