[help]Delete or replace text in multiple file and multiple directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [help]Delete or replace text in multiple file and multiple directory
# 15  
Old 06-05-2009
Quote:
Originally Posted by Nejc
OK. Obviously this script doesn't do, what I would like, since it purged all my files. No harm done anyway, I did it on purpose on a testing website which was also spamed. Smilie

Before I ran the script, I also tested only with sed using this code:

Code:
sed "s:<?php echo '<script language='JavaScript'>function e590206b977().*<\/script>'; ?>::" index.php > index.temp


Surely it won't do anything, since you removed two crucial backslashes before the closing tag \/script>. It won't work without them:

Code:
sed "s:<?php echo '<script language='JavaScript'>function e590206b977().*<\\\/script>'; ?>::" index.php > index.temp


Quote:
I can say, that the above command of sed, just copies the file index.php to index.temp. Nothing else.
There is one thing for sure, every single character counts, if you miss one the whole code will fail. Please copy/paste when testing.

I tested with your spam sample, and it worked fine; obviously, if you have another spam string, different from the one above, the code needs again modifications.

The only important point about sed's pattern is the greedy regex .* , it'll match the whole portion of the string from the end of ...>function e590206b977() pattern up to the beginning of <\/script> spam's closing tag.
# 16  
Old 06-05-2009
It still won't go. I have a normal index.php file added at the end of the file:

Code:
...
JDEBUG ? $_PROFILER->mark('afterRender') : null;
$mainframe->triggerEvent('onAfterRender');

/**
 * RETURN THE RESPONSE
 */
echo JResponse::toString($mainframe->getCfg('gzip'));
<?php echo '<script language='JavaScript'>function e590206b977() jozejsadf</script>'; ?>

In shell I do, for now even without that function part:

Code:
sed "s:<?php echo '<script language='JavaScript'>.*<\\\/script>'; ?>::" index.php > index.temp

Nothing happens. index.temp gets created with the same content as index.php.

Below is a copy/paste of the command:

Code:
demo@soncek:~/demo2$ sed "s:<?php echo '<script language='JavaScript'>.*<\\\/script>'; ?>::" index.php > index.temp


Last edited by Nejc; 06-05-2009 at 11:42 AM.. Reason: Pasted the command
# 17  
Old 06-05-2009
Quote:
Originally Posted by Nejc
...
Nothing happens. index.temp gets created with the same content as index.php.

Below is a copy/paste of the command:

Code:
demo@soncek:~/demo2$ sed "s:<?php echo '<script language='JavaScript'>.*<\\\/script>'; ?>::" index.php > index.temp

I see ..., the previous code ( 3 backslashes ) worked fine in your previous sample, the code will work with the current sample if you remove only one \ .
Anyway to have predictable results, you'd be better off of getting rid of the <\/ pattern from the closing tag.

I get the desired output for all samples with the following code ( note the differences ):

Code:
sed "s:<?php echo '<script language='JavaScript'>.*script>'; ?>::" filename

Tested OK in Solaris and Cygwin (GNU sed).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep and replace multiple strings in a file with multiple filenames in a file

Hi, I have a file containing list of strings like i: Pink Yellow Green and I have file having list of file names in a directory j : a b c d Where j contains of a ,b,c,d are as follows a: Pink (3 Replies)
Discussion started by: madabhg
3 Replies

2. Shell Programming and Scripting

Delete multiple folders in a directory which are two weeks old

I need help. I have to delete multiple directories inside a directory that are two weeks old. Example: Today is July 09, 2012 Folder1 > folder1 (created June 4, 2012) -- should be deleted > folder2 (created June 2, 2012) -- should be deleted > folder3 (created... (4 Replies)
Discussion started by: jasperux
4 Replies

3. Shell Programming and Scripting

Replace directory paths in multiple files at once

I need to update about 2400 files in a directory subtree, with a new directory path inside the files I need to change this occurence in all files: /d2/R12AB/VIS/apps/tech_st/10.1.2 with this: /u01/PROD/apps/apps_st/10.1.3 I know how to change single words using "find . -type f -print0 |... (6 Replies)
Discussion started by: wicus
6 Replies

4. Shell Programming and Scripting

How to replace multiple text in a file using sed

can anyone please help me in the below scenario: File1: Hello1 Hello1 i want to use sed to replace multiple occurances of Hello1 in file 1 to welcome. Thanks a ton for the help (9 Replies)
Discussion started by: amithkhandakar
9 Replies

5. Shell Programming and Scripting

Replace text block in multiple files

I need to replace (delete) a text block in a bunch of files, its a html table, almost at the end of pages but the location varies. In Windows I used Filemonkey, but nothing like that in Unix? There is replace from mysql, but how does it deal with newlines? sed only works with single lines,... (6 Replies)
Discussion started by: eiland
6 Replies

6. UNIX for Dummies Questions & Answers

replace text in multiple files

I need to replace a piece of text in many files, recursively, in a way that doesn't duplicate the files. How would I do that? The closest I've come is grep -rl "text" * | sed -e 's/home1/home2/g' but that just replaces the filename. (2 Replies)
Discussion started by: dhinge
2 Replies

7. Shell Programming and Scripting

Replace text in multiple files

Dear all My task is to replace a strings in multiple files. filename: file1 I can use sed to replace abc.server.com to unix.server.org e.g. sed 's/abc.server.com/unix.server.org/g file1 > newfile1 I have 2 questions. How do I directly save file1 instead of append to newfile1. I... (1 Reply)
Discussion started by: on9west
1 Replies

8. Shell Programming and Scripting

Replace text in multiple files

Ok guys, If anyone could help me out on this puppy I'd be very appreciative! Here's the scenario I have a string for example : <img src=BLANK_IMG border=0 width=221 height=12> or <img src=IMG border=0 height=12 width=221 > or anything else really.... need to basically change each... (10 Replies)
Discussion started by: Tonka52
10 Replies

9. Shell Programming and Scripting

How do you delete multiple text from a comma delimited file

I would like to know code that will delete multiple text from a comma delimited file. For example, how would the comma delimited file below delete the word 'PEST' in Perl language (previously an excel file that was converted to a csv and the last column was PEST): 1, 2,43,34, bosx,PEST 1,... (1 Reply)
Discussion started by: dolo21taf
1 Replies

10. Shell Programming and Scripting

Need to search and replace in multiple files in directory hierarchy

Hello all I need to search and replace in multiple files that are in directory hierarchy Im using the : find . -name "*.dsp" -print | xargs grep -n -o Test.lib" , I like to be able to replace every instance of Test.lib with empty space . how can I write one liner that does this ? (3 Replies)
Discussion started by: umen
3 Replies
Login or Register to Ask a Question