Multiple passes
Based on the extra information about the problem and it's structure I would advise following steps:
- Split every line in two, right before the second pattern.
- Grab the desired part from every line.
- Recombine the lines so matches are on their original line.
This means something like this:
- Replace
^(\d*\s*)(\d*\s*)$
with $1\r\n$2
. Simply drop the \r
if you're not on windows, which I doubt. You should perhaps think of a macro to add at the end of a line. This should be something that is not included in the rest of the document (for instance #
). The $1
means, replace the first captured group (stuff inside brackets). So replace it with $1#\r\n$2
.
- Now grab the desired length of each line:
(^.{n}).*(#?)
and replace with $1$2
. This will capture the first n
symbols and insert the macro if it is found.
- Remove newlines after macros:
#\r\n
. Either remove these or replace them with \0
.
Notes
- You'd have to filter the lines matching
(^\d*\s*)
first.
- If you'd like another macro, sub the occurrences of
#
in above the answer. It should not be contained in the rest of the file, at least not at the end of a line.
- This answer uses backreferences, which should be no problem.
Single pass
A single pass might be possible too here.
^(\d[\d\s]{n-1})[^\d]*(\d[\d\s]{n-1}).*$
Matches these lines, if you extract group one and two, this will filter the desired output from the file. Simply substitute it for $1$2
.