regular expressions in exec sed - regex

Could you help me with regular expressions in exec sed?
Example code:
<?php echo "This code need to delete"; ?><? echo 'This code need to keep'; ?>
I need to delete:
<?php echo "This code need to delete"; ?>
In all files, and keep
<? echo 'This code need to keep'; ?>
I tried to do it like this:
find ./ -type f -name \*.php -exec sed -i -r 's/<\?php.*\?>//g' {} \;
But this way doesn't work correctly. (delete all code)

Use a negated character class instead of .* because .* is greedy which matches any character as much as possible.
find ./ -type f -name \*.php -exec sed -i -r 's/<\?php[^>]*\?>//g' {} \;
You could use -name '*.php' instead of -name \*.php in the above.
Example:
$ echo '<?php echo "This code need to delete"; ?><? echo 'This code need to keep'; ?>' | sed -r 's/<\?php[^>]*\?>//g'
<? echo This code need to keep; ?>

Using gnu awk you can do this to get rid of first <?php...?> block:
cat file
<?php echo "This code need to delete"; ?><? echo 'This code need to keep'; ?>
awk -v RS='\\?>' '!/<\?php /{printf $0 RT}' file
<? echo 'This code need to keep'; ?>

Related

Break string up and section to the end

I am using a sed command to find /controller which is a word/action
"href="/bdgp/bdgp-record-com1-form?type=R&year=<?php echo $year;?>&batchNumber=<?php echo $this->escapehtml($animal->BATCH_NUMBER);?> "
which changes the result like so...
href="<?php echo $this->url("bdgp", array("action"=>"bdgp-record-com1-form"))?>?type=R&year=<?php echo $year;?>&batchNumber=<?php echo $this->escapehtml($animal->BATCH_NUMBER);?>
This is the sed command I am using which finds the pattern like /anything/anything/anything
sed -i 's%href=\"\/\([^/]*\)\/\([^/]*\)?\([^/]*\)%href=\"<?php echo $this->url(\"\1\", array(\"action\"=>\"\2\"))?> \"<%g' changes.txt;
What I get though is this...
href="<?php echo $this->url("bdgp", array("action"=>"bdgp-record-com1-form?type=R&year=<?php echo $year;?>&batchNumber=<?php echo $this->escapehtml($animal->BATCH_NUMBER);?>
How would I extract out from the result the ?type=R&year=&batchNumber=escapehtml($animal->BATCH_NUMBER);?> and put it on the end?
Thanks

Globally replacing short tags from all pages

I was looking to build a sed that would globally kill short tags in my scripts as there is a LOT of legacy stuff floating around that needs to be banished. I was working of a regex but it's being greedy so am looking for a non-greedy sed that will replace all occurances of
<\?=(.*)\?> ## Pattern
<?php echo $1; ?> ## Replacement
This WILL turn <?=$foo?> into <?php echo $foo; ?> but it will also turn <?=$foo?><?=$bar?> into <?php $foo?><?=$bar; ?> as well as duplicates the line of the match.
The SED looks like sed -i -e "s/<?=\(.*\)?>/<?php echo \1; ?>/g" file
My answer would assume that
<?=$foo?><?=$bar?> ## in
<?php echo $foo; ?><?php echo $bar; ?> ## out
You can try with
sed 's/<[?]=\([^?]*\)/<?php echo \1; /g' file

Regular expression search and replace across whole directory in Linux terminal

In my PHP project, I have a PHP function that does some language stuff and is being called as:
<?php echo __('STRING'); ?>
I would like to switch from the consistent usage of uppercase string indexes, to a consistent usage of lowercase string indexes, so I would like to replace all these occurances:
__('SOMETHING')
With:
__('something')
What would be the command to do this?
I have a command ready for easy search & replace functionality, but I don't know how to write the regex.
find . -name "*.php" -print | xargs sed -i 's/search/replace/g'
You can use -print0 with xargs -0:
find . -name "*.php" -print0 | xargs -0 -I {} sed -i.bak 's/search/replace/g' {}
Use strtolower function.
Example:-
<?php
echo strtolower("Hello WORLD.");
?>
Result:-
hello world.

search and replace files in linux(sed)

I'm trying to search and replace the following:
<?php
<!DOCTYPE HTML>
with
<!DOCTYPE HTML>
so far I have tried this:
find . \( -name "*.php" \) -exec grep -Hn "<?php <\!DOCTYPE HTML>" {} \; -exec sed -i 's/<?php <\!DOCTYPE HTML>/<\!DOCTYPE HTML>/g' {} \;
But it's not finding any instances of files with my needle string which exists on my server.
find . -name "*.php" -exec grep -lZz '^<?php[[:space:]]\+<!DOCTYPE HTML>' {} + |
xargs -r0 sed -i '^<?php[[:space:]]*$/,1d'
Edit: The previous version didn't work due to the character \n in the pattern. The updated version avoid this character.
With GNU awk (for RS='\0' to read the whole file as one record) and assuming your file names don't contain newlines all you need is the clear, simple:
find . -name '*.php' -print |
while IFS= read -r file; do
gawk -v RS='\0' '{gsub(/<\?php\n<!DOCTYPE HTML>/,"<!DOCTYPE HTML>"); print}' "$file" > tmp &&
mv tmp "$file"
done

complex batch replacement linux

I'm trying to do some batch replacement for/with a fairly complex pattern
So far I find the pattern as:
find '(' -name '*.php' -o -name '*.html' ')' -exec grep -i -n 'hello' {} +
The string I want to replace is currently as follow:
<img src="/some/path/to/somewhere/hello" />
where the path for the image varies but always contain the sub-string 'hello' at the end
I would like to grab the path and perform a replacement as follow:
<img src="<?php myfunction('(/some/path/to/somewhere/)'); ?>" />
What would a good way to perform this?
Any help will be appreciate it.
Replace -exec grep ..... with
-exec cat '{}' | sed s/<img src="(/some/path/to/somewhere/)hello" />/<img src="<?php myfunction('(\1)(constant)'); ?>" />/ > /tmp/output; mv /tmp/output '{}' ';'
escaping spaces, " and / symbols in sed's search and replacement patterns with backslahes as sed likes.
For instance, this will extract /path/:
echo "<img src=\"/path/hello\"" | sed "s/<img\ src=\"\(\/path\/\)hello/\1/"