On 03/22/2011 03:33 PM, 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

A larger example would be the file

http://www.rfc-editor.org/in-notes/rfc-index.txt

with the command

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

The first match 1129 from line 24 is prepended to every following line.

Where is the problem?

The "-" :)

Walk through the substitution sequentially and you'll see what's happening:

The first line gets changed to "01:00 x", then the 2nd line looks in the previous line (now containing the "01:00" you just prepended) and prepends that matching "01:00"...lather, rinse, repeat.

The following worked for me:

:%s/^/\=substitute(getline(line('.')-1), '^\%(\d\{2}:\d\{2} \)\=\(\d\{2}:\d\{2} \).*', '\1' ,'g')

-tim



--
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