3

I have a log file for what should be simple counter values, formatted as:

0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
etc...

I'm looking for a way to find if there's anywhere in the log where the counter glitched, for example:

0 3 2 3 4 5 6 7

The 0 3 2 part is erroneous and what I'm looking to find. Is there a way to do this in vim? Eg, something along the lines of /\(\d\+\)\s\(\=submatch(1)+1\)\@!

I know that vim supports \=submatch() but that can only be used in the :substitute command.

8

I doubt Vim's regex's are powerful enough to handle that kind of pattern. However you can use any kind of generic programing language to fit your need. Here is an awk version:

:%!awk '{for(i=1;i<=NF;i++) { if(i+$1-1\!=$i) { print; break;}}}'

Using the ! command to filter the entire file, %, with an awk program that verifies each field is ascending correctly.

Note: the ! in != needs to be escaped as it has special meaning to vim.

For more help see:

:h filter
:h range
:h :!
3

Unlike Perl, Vim does not allow Vimscript expressions inside a {pattern}, so you're limited to textual interpretation. With a sufficiently complex pattern, you could do some such assertions (using look-behind and look-ahead, as in 1 not preceded by start-of-line or 0, not succeeded by end-of-line or 2; or 2 not preceeded by ...), but I would not recommend that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.