Something like this would be a real PITA to work out how to do in a completely automated way. It's a very large, complex and specific pattern of text to try to look for.
Doing it manually would probably be the quickest and safest way.
Using a text editor you could perform a search for occurrences of the
<script>
start tag. Then whenever you find a tag that contains your offending code, you select it and delete it.
Then repeat the search through the rest of the document, in case it's there more than once.
In any editor, it would be trivial to perform and repeat a search for a
<script>
start tag, but the text selection would be a very manual process, unless you used a more powerful editor like Vim or emacs - which makes doing things like this a doddle.
Personally, as an experienced Vim user I'm happy to share how I'd solve this using Vim for a repetitive task like this.
I'd use the following workflow:
1. Load ALL of the files into buffers in vim
e.g.
2. Enter a command to search for a script start-tag:
3. Press
n
(next result) until I find the
<script>
tag containing the "bad" code.
4. Once I've found the first occurrence of the tag containing the bad code - I will press the following key commands:
qtdatq
q - tells Vim we want to start recording a macro
t - tells Vim to store the macro in register t
d - tells Vim that we want to delete something
a - means "all"
t - means "in the tag"
q - means "end the macro"
So this basically tells vim - "Record a macro in register t; Delete everything in the current tag (including the start/end tags and everything in between); end of macro"
So now we have successfully recorded a macro into register t that will delete an entire tag. That will delete everything from the
<script>
tag that is currently under the cursor, to its corresponding
</script>
tag.
And if you manually move the cursor to any other tag, running/replaying the macro would also delete that entire tag.
To replay the macro, all we have to do is press
@t
and that will repeat the
dat
keypresses that we recorded in the macro (Delete all in tag).
If you accidentally delete the wrong tag, use
u
to undo the last action.
5. Once I've deleted the code (or reached the end of the file without finding anything) - I'd enter the following commands:
:w
- to save the file
:bnext
- to switch to the next file/buffer.
Now we're working on the next file - the search is already set up, so it's just a case of hitting
n
to repeat the search and move to the next
<script>
tag, and the next, until we find the tag containing the "bad" code.
Then we simply press
@t
to replay the macro and delete the tag.
Again, If you accidentally delete a tag that you didn't mean to delete, then press the
u
key to undo that action.
Once you get to the end of the second document, save it with
:w
and move to the next document with the
:bnext
command.
Rinse and repeat as many times as necessary!
Granted, it's still a repetitive, manual task.
But in vim, because of its powerful command mode - once you've performed your initial search and recorded the deletion macro - the rest of the task becomes a handful of short, simple keypresses.
Hit
n
until you find the tag with the bad code, type
@t
to delete the entire tag (via the macro stored in register t). Save and move to the next file.
Compare that to an ordinary editor like nano - the search step would be the same - all editors have search functionality - so that would involve hitting a shortcut key and entering a search term and hitting a key to move to the next result - that's kinda universal to editors.
But when it comes to selecting and deleting the text - in most ordinary editors, you'd have to manually (and/or visually) select the block of code each time and then manually delete it.
Which could involve a lot of keystrokes, or using the arrow keys, or the mouse etc. Which again, is far more inefficient than a few simple keystrokes.
In vim it would just be a case of typing
dat
, or using the
@t
macro we recorded to perform the selection and deletion. So you're looking at two or three keystrokes maximum for each selection/deletion.
Anyway - that's how I'd solve this problem using Vim. And the explanation probably took me longer to write than it would to actually perform the task itself.
Maybe I should make a video or something?!
And my emacs skills are extremely rusty. I haven't used emacs for a long time. I gave up using emacs because I have arthritis in my hands and its default keybinds physically hurt me! Ha ha!
But perhaps an emacs user can share their workflow for efficiently performing this task!