Amir
Amir

Reputation: 1428

How to check if no two lines containing the same strings in VIM

I would like to know how am I gonna be able to check if in my 1000 lines txt-file, no two lines have the same pattern. in the third section, namely, "CF.Name" i.e. I have something like :

 APPNAME            NO  CF.Name
 automotive_bitcount 1 -funsafe-math-optimizations -fno-guess-branch-probability    
 automotive_bitcount 1  -fno-guess-branch-probability   -fno-inline-functions -funroll-all-loops -O2 
 security_blowfish_e 2 -funsafe-math-optimizations  -fno-ivopts    -O2
 security_blowfish_e 2 -funsafe-math-optimizations  -fno-ivopts -fno-tree-loop-optimize  -funroll-all-loops -O2

and so on. Just wanna make sure all the generated CF.Name are different from each other.

Upvotes: 2

Views: 164

Answers (3)

benjifisher
benjifisher

Reputation: 5122

I would take parts of the other answers: use :sort as in @Jeff's answer, but use a pattern as in @Ingo Karkat's answer instead of deleting the other columns:

:sort /^\S\+\s\+\S\+\s\+/

Now for something new: a multi-line pattern. If you want to find two consecutive lines that are identical, use /^\(.*\)\n\1$. If you care about the third column (delimited by whitespace) then search for

/\v^\S+\s+\S+\s+(\S+).*\n\S+\s+\S+\s+\zs\1\S@!

For variety, I have used the \v ("very magic") variant. (This way the + and @! do not need to be preceded by \.) Also, I slipped in \zs ("start here") so that you highlight only the interesting part.

Upvotes: 2

Jeff
Jeff

Reputation: 1807

Delete all columns except the one you want to check, then :sort u. If the result is 1000 lines, then you know that no two lines have the same pattern in that section.

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172648

My PatternsOnText plugin has a :PrintDuplicateLinesIgnoring command that searches for duplicate content in lines while skipping over certain parts. In your example, you want to ignore the first two sections, which can be matched by start of line, some non-whitespace, some whitespace, some non-whitespace, some whitespace. Ergo:

:PrintDuplicateLinesIgnoring /^\S\+\s\+\S\+\s\+/

Note: This still won't find lines where the same arguments are given in a different order; hopefully, your creation process ensures that, or you'll have to sort the arguments first.

Upvotes: 3

Related Questions