I've got a file, containing some output log. It consists of several blocks of information, that start and end with some fixed patterns.
I need to put a summary for each block in the beginning of the file.
Additionally, I'd like to study vim scripting.
I've developed a couple of functions: one of them processes line range and returns a string with the summary, and another one puts this string in the first line of a file.
function! GetBlockSummary(first, last)
" some text processing
endfunction
function! ProcessBlock() range
let summary = GetBlockSummary(a:firstline, a:lastline)
call append(0, summary)
endfunction
Then I call them: :/startpattern/;/endpattern/ call ProcessBlock()
Now I've got two problems:
- There are many blocks in the file, and it is tedious to repeat the command by hands. This can be solved with putting the call into a macro and then calling that macro with count prefix (say,
90@q
, if I have known that there are 90 blocks in the file, and have put the call into the@q
register) - But different files have different amount of blocks, and I have to call
grep
to count them.
So, I'm looking for some while
loop example, implementing following pattern:
while /startpattern/ is found
execute :/startpattern/;/endpattern/ call ProcessBlock()
endwile
Here is the example.
Original file:
start/weights-e55_ta0.98_va0.96.ckpt
Extracting frames
Using precomputed keys
Done, 1247 frames
acc1 1
acc2 2
acc3 3
acc4 5
end
start/weights-e18_ta0.84_va0.12.ckpt
Extracting frames
Using precomputed keys
Done, 1247 frames
acc1 5
acc2 2
acc3 3
acc4 4
end
Each block starts from start/....
and ends with end
. Numbers after "e" in weights-e...
denote block numbers.
For this example I'd like to have this file prepended with block numbers and acc
's that equal 5. It is guaranteed that only one acc\d 5
appears in the block:
Block 55: acc4
Block 18: acc1
start/weights-e55_ta0.98_va0.96.ckpt
Extracting frames
Using precomputed keys
.... the rest of text