Sublime Forum

Selecting multiple lines with a pattern

#1

Hello everybody, I tried to figure out by myself but can’t find it, maybe someone can help me please?

I want to copy the lines from 10k lines document in this particular order/pattern

Copy/select the content of the line every 3rd line, 3rd line, 3rd line, 4th line
then again copy 3rd line, 3rd line, 3rd line, 4th line for the whole document.

I’m using this Regex code to select every third line, but can’t figure out how this order 3 3 3 4.

.\n.\n.*\n

Could this be achieved in Sublimetext?

Thanks

0 Likes

#2

I would say regex is a wrong tool for the job. Here is a simple python script for it.

import itertools

every_n = [3, 3, 3, 4]
accumulated_n = list(itertools.accumulate(every_n))  # [3, 6, 9, 13]

selection_period = accumulated_n[-1]  # 13
selected_n = {n % selection_period for n in accumulated_n}  # {3, 6, 9, 0}

with open("input.txt") as f:
    for n, line in enumerate(f, start=1):
        if n % selection_period in selected_n:
            print(line, end="")

0 Likes

#3

Hi jfcherng, I don’t really know how to use python, is there no other way?

Or at least guide me what version of pyton do I need to, and how to use it? , I have little knowledge about it.

Thanks for your efforts,

Best Regards

0 Likes

#4

If you are not familiar with Python, I am afraid that won’t be easier. I won’t help about that but just wait other people to provide another solution. Sorry.


Or maybe you are not on Windows and know how to run a command in a terminal:

awk 'NR%13==3 || NR%13==6 || NR%13==9 || NR%13==0' INPUT_FILE > OUTPUT_FILE
0 Likes

#5

Windows user,

I’ll wait to see if other can help,

Thanks

0 Likes

#6

I do this type of thing quite often, just usually not line number based; so here is my slightly crazy approach without script/python execution: (I’m on a Mac, some shortcuts may be wrong)

  1. open a cursor at the beginning of each line (find: ctrl+f; search regex: ^; find all: alt+enter)
  2. run Arithmetic in command palette ctrl+shift+p
  3. type formula (i % 13) + 1 & press enter (FYI, i is the line number)
  4. type the space key
  5. open find search for ^(3|6|9|13)\b (or space at end, markdown isn’t being helpful here)
  6. find all: alt+enter
  7. two right arrow keys (one if space is used in step 5)
  8. select the rest of the line ctrl+shift+right
  9. copy+paste into new file

There might be a package somewhere that could do add computationally based selections, but I didn’t find any on a cursory glance.

edit: I haven’t dived into running commands in the ST console, but I would imagine there is a way there to do this without requiring editing the file.

0 Likes

#7

Hello srbs, very smart way, I see what you did there. I add numbers 1-13 to every 13lines and select the lines after using those numbers as pins.

Thank you very much, very helpful

0 Likes