0

I have a block of php code (inside <? php ... ?>) in multiple files which I want to remove.

I tried to put the code into a variable:

var="<?php [some code in here] ?>"

Then run this:

find . -type f -name '*.php' -exec sed "s|$var| |" {} \;

Or

find . -type f -name '*.php' -exec sed -i 's/'"$var"'.*/'"$str"'/g' {} \;

(where str=" ")

Is there a way to do this?

4
  • 1
    You got success and looking for another way(s)? or you couldn't get success with above ones? BTW both working just fine May 1, 2018 at 5:15
  • Somehow none of the above works for me. :( None of them could replace the string.
    – Vu Hoang
    May 1, 2018 at 5:21
  • 1
    Is this on one line, or multiple? Are there special characters in the php element (relative to bash; $, !, ", ')? May 1, 2018 at 9:20
  • You'll also run in trouble if your php code contains / or . or [ or * or something else that breaks your sed script. We could better help you if you tell us the actual code to be removed.
    – Philippos
    May 3, 2018 at 12:00

1 Answer 1

0

Try this, but remove the -i the first time to make sure it works:

var='echo rand(1, 10);'  # dummy code, change to whatever.
sed -i 's/<? php '"${var}"' ?>//' *.php

Notes:

  • The code in $var is assumed to be a one-liner.
  • If the code contains any ' single quotes, escape them like so \'.
  • The above assumes whitespace variation isn't a problem, but it often can be.
1
  • Thanks for your help! But what I mean is there are more code in that "....". My bad not making it clear.
    – Vu Hoang
    May 1, 2018 at 8:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .