Sublime Forum

Replace Selection (between Begin and End)

#1

Hello. I have a bunch of files that I want to change. The text that interests me is somewhere in the middle of each file. Let’s say that my text is between 250 and 370 row. I want to keep this text and delete the rest of other lines in all file/files. Here’s how it looks.


  1. DOCTYPE html

.
.
250 some-css -code-H2
.
First World TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT Last Word
.
370. container-div
.
.
500. /html>


I have two possibilities that I might ever do, with 3 fields

The first possibility Select everything before Text and delete.

Field 1. Search and Select BEGIN: “DOCTYPE html”
Field 2. Search and Select END “some-css -code-H2”
Field 3. Replace with 0 (Nothing)

The same text after

The second possibility Select the entire text version which lies between

Field 1. Search and Select BEGIN: some-css -code-H2
and
Field 2. Search and Select END container-div
Field 3. DELETE ALL REST (or) RAPLACE WITH 0 (nothing)

Someone help me?

0 Likes

#2

Some possibilities from off the top of my head, arranged from least automated to most automated:

  • Select the text you want to keep and then do “Selection > Invert Selection” from the menu (I think this is built-in to ST3, but if you can’t find it let me know; maybe it’s in one of my installed packages)
  • Record a macro to do this for you
  • Write a plugin to do this for you
0 Likes

#3

hello. what exactly, what macro-code to record? Sounds good, but I am not really a programmer, I am not very good at this.

Your idea with “Selection > Invert Selection” is somehow good, but I don’t know how to do this in 500 files at once.

By the way, every text is different on every page, it is not the same, cannot record a macro on a specific text. If that is your suggestion.

0 Likes

#4

Hmm, 500 files is quite a lot. I personally might write a Python script to do it for me but you say you’re not really a programmer so that might not be an option.

Macros are an easy way to do repetitive tasks multiple times. They have some limitations but I think they should work for you. With macros, you can probably do this in half the time it would take to do it manually without macros, especially if you use keyboard shortcuts to run the macro.

Here are the basics:

  1. Start recording a macro with “Tools > Record Macro” in the menu
  2. Edit your file (for you, this means: delete the first 249 lines of text, then move forward 120 lines, then delete the rest of the file)
  3. Stop recording the macro with “Tools > Stop Recording Macro” in the menu
  4. Go to your next file you want to edit
  5. Run the macro with “Tools > Playback Macro” in the menu
  6. Repeat steps 4 and 5 until you’re done with all files

Limitations of this method:

  • the text must always be exactly between lines 250 and 370. If this isn’t the case, you could record a macro that deletes everything not currently selected, and then to use it you would need to select the text you want to save before running the macro in each file.
  • you have to run the macro 499 times, which will take a while

Before you do anything, make sure to make a backup of your files! Disk space is cheap and automated tasks like this can easily go wrong

0 Likes

#5

yes, I know macros, is like a Copy-Paste method (Ctr+V).

But what I want to do is to keep the texts from .html files, so that to cancel all the rest stupid code on the pages, which is very long.

imagine a poem surrounded by a bunch of scripts. I want to delete those scripts and preserve the poem in each page.

0 Likes

#6

Gotcha. Using a macro still seems like the easiest thing to do, if it works. Otherwise, you’ll want to google around for “web scraping tutorials”, which will involve programming

0 Likes

#7

The following method should work.

 

Recommendations:

  • Use a duplicate set of the original files for testing

  • Test this on 10 files first, then 100, then 500, etc. to see how it scales performance-wise
     



 

  •   Open the find_in_files panel with Ctrl + Shift + F

  • use the RegEx pattern in the next section as your Find: argument

  • use C:\YourDirectoryPath\ThatContains\TheFilesToEdit as your Where: argument

  • leave the Replace: field blank

  • make sure the RegEx option is enabled

  • Click the replace button
     



 
Here is the RegEx Pattern I used with your example:

(?s)((^.*)(250 some-css -code-H2)|(container-div)(.*$))

 
The same pattern with placeholders, in case you need to modify it:

(?s)((^.*)(delete_until_this_text)|(delete_after_this_text)(.*$))

 

BreakDown:

(?s)                          # enable [ dot all | single line ] mode
(
   (^.*)                      # match all text from the start of the document
   (delete_until_this_text)   # until this text is found
   |                          # OR
   (delete_after_this_text)   # match this text
   (.*$)                      # until the end of the document
)
1 Like

#8

yes, I will try the RegEx Patter, thanks a lot.

In the mean time, I discover a method that works, that modify a lot of files at once. It is a small TExt Editor “multiple file search & replace”. There are 2 operation: delete from one point to another before text (Replace with Nothing). And delete after text from one point to another after text (replace with nothing). As you can see in this image.

0 Likes

#9

In Sublime, Find in Files gives you similar options to the above, across multiple files. Using regex gives you greater flexibility to match patterns than the above, but you should be able to achieve much the same in Sublime. The only thing is that Sublime will attempt to open every file, so if you’re working with a huge amount of files, another tool like the one above might be a better option for you.

0 Likes

#10

Hi, fico. Your RegEx code works WONDERFUL ! Super Good ! Thanks a lot !

by the way, can you give me a clue, where can I learn easy way RegEx?

1 Like

#11

 
See my post in this thread.

Also, jfcherng’s post in the same thread has a tutorial that might be worth checking out.

0 Likes