On Tue, 22 Mar 2011, Dennis Benzinger wrote:

Hi!

I have a log file with timestamps and messages:

01:00 x
02:00 y
03:00 z

To compare the time elapsed between two events I want to prefix each line with the timestamp of the previous time. I tried to use

%s/^/\=matchstr(getline(line(".")-1), "\\d\\{2}:\\d\\{2} ")

but somehow the first match is prepended to every line:

01:00 x
01:00 02:00 y
01:00 03:00 z

By the time the expression is evaluated, you've prepended the timestamp from two lines prior to the prior line.

So, using your example:

line(1)=="01:00 x"
line(2)=="02:00 y"
line(3)=="03:00 z"

On line (1) the matchstr() doesn't prepend anything, since nonexistent line(0) doesn't match the timestamp pattern.

On line (2) the matchstr() prepends line(1)'s "01:00 ".

When it reaches line(3), line(2) == "01:00 02:00 y". So, it grabs the "01:00 " again.

I'm not sure if there's a way to specify that a range should be reversed. But, you could also substitute it in such a way that it no longer matches the pattern:

:%s/^/\='_GUARD_PATTERN_'.matchstr(getline(line(".")-1), 
'\%(_GUARD_PATTERN_\)\@<!\d\{2}:\d\{2} ')
:%s/_GUARD_PATTERN_//

--
Best,
Ben

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to