Jump to content
Ritter

Massive Search and Replace

Recommended Posts

find ./ -type f -iname '*.html' -exec sed -i.backup -e :a -e 's/<!-- Begin Nedstat Basic code -->.*<!-- End Nedstat Basic code -->//g;/</N;//ba' {} \;

 

don't just run this without thinking ... there are key elements to understand as I will explain.

 

Firstly, the outside wrapper, is the program we use to literally find all the files we want to include in the search. It is given a few arguments, the first "./" is the path and IT IS RECURSIVE, which means ... current directory and any with in it. The next argument "type f" tells it only to select files. The next argument limits to (case in-sensitive, thanks to iname rather than name) *.html lastly we tell find for each result to perform an operation, another shell command, which is the sed command you see next in line.

 

I don't know nearly enough about sed to explain all this in great detail, but the basic idea is a multiline search and replace. In this case, for Anyweb, we have been given a strict start and end delimiter as you see represented as html comment tags. Everything in between them is replaced but what in between the second set of /s .. in this case, its nothing, so this is just a strip. The key to the sed command comes from using the find command with an -exec argument, {} is the file name and path to the file selected by find, and the \; effectively tells find that the exec statement is over and done.

 

Oh, as a safety, the -i.backup passed to sed tells sed to save a copy of the original file as filename.html.backup

 

I hope that helps you Anyweb!

Share this post


Link to post
Share on other sites

thanks a Million Ritter,

this is great,

 

i'll do a test run on it shortly and report back any issues I have with it,

 

once again, thanks a million !

 

cheers

anyweb

Share this post


Link to post
Share on other sites

hey ritter dude !,

 

what an excellent script !

 

works brilliantly and seems to have done the job (only time will tell)

 

now.... how do i get rid of all those 'backup' files it left behind ? (loads of them! see sample below)

 

[root@www mycar]# ls

car mycar (2).html mycar (55).html

hubs mycar (2).html.backup mycar (55).html.backup

images mycar (30).html mycar (56).html

index1.html mycar (30).html.backup mycar (56).html.backup

index1.html.backup mycar (31).html mycar (57).html

index2.html mycar (31).html.backup mycar (57).html.backup

index2.html.backup mycar (32).html mycar (58).html

index3.html mycar (32).html.backup mycar (58).html.backup

index3.html.backup mycar (33).html mycar (59).html

index4.html mycar (33).html.backup mycar (59).html.backup

index4.html.backup mycar (34).html mycar (5).html

index.html mycar (34).html.backup mycar (5).html.backup

index.html~ mycar (35).html mycar (60).html

index.html.backup mycar (35).html.backup mycar (60).html.backup

mycar (10).html mycar (36).html mycar (61).html

mycar (10).html.backup mycar (36).html.backup mycar (61).html.backup

mycar (11).html mycar (37).html mycar (62).html

mycar (11).html.backup mycar (37).html.backup mycar (62).html.backup

mycar (12).html mycar (38).html mycar (63).html

mycar (12).html.backup mycar (38).html.backup mycar (63).html.backup

mycar (13).html mycar (39).html mycar (64).html

mycar (13).html.backup mycar (39).html.backup mycar (64).html.backup

mycar (14).html mycar (3).html mycar (65).html

mycar (14).html.backup mycar (3).html.backup mycar (65).html.backup

mycar (15).html mycar (40).html mycar (66).html

mycar (15).html.backup mycar (40).html.backup mycar (66).html.backup

mycar (16).html mycar (41).html mycar (67).html

mycar (16).html.backup mycar (41).html.backup mycar (67).html.backup

mycar (17).html mycar (42).html mycar (68).html

mycar (17).html.backup mycar (42).html.backup mycar (68).html.backup

mycar (18).html mycar (43).html mycar (69).html

mycar (18).html.backup mycar (43).html.backup mycar (69).html.backup

mycar (19).html mycar (44).html mycar (6).html

mycar (19).html.backup mycar (44).html.backup mycar (6).html.backup

mycar (1).html mycar (45).html mycar (70).html

mycar (1).html.backup mycar (45).html.backup mycar (70).html.backup

mycar (20).html mycar (46).html mycar (71).html

mycar (20).html.backup mycar (46).html.backup mycar (71).html.backup

mycar (21).html mycar (47).html mycar (72).html

mycar (21).html.backup mycar (47).html.backup mycar (72).html.backup

mycar (22).html mycar (48).html mycar (73).html

mycar (22).html.backup mycar (48).html.backup mycar (73).html.backup

mycar (23).html mycar (49).html mycar (74).html

mycar (23).html.backup mycar (49).html.backup mycar (74).html.backup

mycar (24).html mycar (4).html mycar (75).html

mycar (24).html.backup mycar (4).html.backup mycar (75).html.backup

mycar (25).html mycar (50).html mycar (7).html

mycar (25).html.backup mycar (50).html.backup mycar (7).html.backup

mycar (26).html mycar (51).html mycar (8).html

mycar (26).html.backup mycar (51).html.backup mycar (8).html.backup

mycar (27).html mycar (52).html mycar (9).html

mycar (27).html.backup mycar (52).html.backup mycar (9).html.backup

mycar (28).html mycar (53).html mycar.html

mycar (28).html.backup mycar (53).html.backup mycar.html.backup

mycar (29).html mycar (54).html newcar

mycar (29).html.backup mycar (54).html.backup updates

heh :)

 

in addition, this method works SO well that I'd like another alternative to what it does,

 

ie: i'd like a script to ADD content to every HTML file that it finds

 

can you help me with that ?

 

cheers

anyweb

Share this post


Link to post
Share on other sites
now.... how do i get rid of all those 'backup' files it left behind ? (loads of them! see sample below)

easiest way:

 

find ./ -type f -iname '*.backup' -exec rm -v {} \;

Share this post


Link to post
Share on other sites

Same technique anyweb .. just replacing the search and replace sections:

find ./ -type f -iname '*.html' -exec sed -i.backup -e :a -e 's/\(FIND ME\)/\1ADD ME AFTER FIND ME/g;/</N;//ba' {} \;

 

should do it

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...