aspose file tools*
The moose likes Linux / UNIX and the fly likes Recursive code in files .. Need to delete the code only but leave the file intact Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Recursive code in files .. Need to delete the code only but leave the file intact" Watch "Recursive code in files .. Need to delete the code only but leave the file intact" New topic
Author

Recursive code in files .. Need to delete the code only but leave the file intact

John Roberson
Greenhorn

Joined: Mar 07, 2013
Posts: 2
I'm a script newbie and need help in deleting malicious code recursively

the code is nested between the following in at least 100 files..
Any way of deleting the code ..


<?#68c8c7#
echo " $

#/68c8c7#?>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16057
    
  21

Welcome to the JavaRanch, John!

Yes, there are multiple ways of doing that. One of them is to write a "sed" script that scans down to your opening offender line and deletes until it has matched the closing offender line. You can use the "find" command combined with "xargs" to run sed (and the sed script) for each file of interest.

Perl can also help do stuff like that.


Customer surveys are for companies who didn't pay proper attention to begin with.
John Roberson
Greenhorn

Joined: Mar 07, 2013
Posts: 2
I tried this

find /hsphere/local/home/dennisch/ -name "*.php" -exec sed -i "/<?68c8c7#/,/68c8c7#?>/d" '{}' \;

But it gives me a lot of

sed: couldn't edit /hsphere/local/home/dennisch/components: not a regular file
sed: couldn't edit /hsphere/local/home/dennisch/components/com_mailto: not a regular file

And doesnt delete the code

Any ideas as to what I'm doing wrong?
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

John Roberson wrote:Any ideas as to what I'm doing wrong?

Fortunately, yes. :-)

John Roberson wrote:I tried this
find /hsphere/local/home/dennisch/ -name "*.php" -exec sed -i "/<?68c8c7#/,/68c8c7#?>/d" '{}' \;


The problem is that the shell sees your characters within the "" and the < and > characters are special to the shell (used to redirect input and output respectively). Replace all the "" in your command line with ' and you should be fine.

Personally, I don't like the -i option. I would like to see the results of sed written out to a different file, and maybe have the option of going back to the original version of the file (the version before sed modified it) -- YMMV.

- Anand

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursive code in files .. Need to delete the code only but leave the file intact