Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to located all lines in a file where the | (pipe) character occurs more than 21 times. The | character is a field delimiter, so there is no specific number of characters that may fall between | instances. Is there an easy way to do this in Vim?

NOTE: I am using gVim on a Windows machine, so I won't be able to use any *nix specific tricks.

TIA

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can use a regexp to find such lines with the normal search function:

/\v(\|[^|]*){21,}

The \v makes sure the regexp works regardless of the value of the magic option (it makes all non-alphanumeric characters (except underscore) special; I also use it here to avoid having to put backslashes before the (, ), {, and }).

If you want to see all such lines at once, then you can combine it with the :g command:

:g/\v(\|[^|]*){21,}/p

Incidentally, this type of command (from the ex predecessor to vi) is the origin for the name of the grep tool (g/re/p: re short for regexp).


If your source data is “pipe delimited”, then you should probably check to see if any of the values have embedded pipe characters (maybe done by escaping (e.g. prefixed with a backslash), doubling, or quoting). The regexp method above can be extended to cover most escaping and doubling methods, but parsing quoted values usually requires power expressive power than most regexp languages can offer.

share|improve this answer
    
The file is pipe delimited and the problem is that there are some non-escaped pipe characters found in some fields that should not be there (causing an issue with a program that is based on the assumption that there are no pipe characters unless they are being used as a delimiter). I wanted to go through the file to get an idea of how many lines were "illegal". Anyway, your answered worked great. Thanks for the help. –  Jason Down Oct 7 '10 at 14:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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