jxn
jxn

Reputation: 8025

vim count number of lines not containing a pattern

Given a CSV file below, how can i use vim to count the number of lines that do not contain: fff or sss

aaa bbb ccc ddd eee, 3221
aaa ddd eee fff, 3222
fff ddd sss www aaa, 3223
ggg www qqq, 3224
sss aaa vvv, 3225

I have this but it only matches some and not all

%s/\v^(fff|sss)//gn

Upvotes: 3

Views: 949

Answers (2)

mMontu
mMontu

Reputation: 9273

The ^ anchors in the start of the line, so it match lines starting with fff or sss. It is a sort of negative only when inside square brackets ([^a] matches anything except a).

You are looking for something like this:

%s/\v^((fff|sss)@!.)*$//gn

More info at :help @!.

Another solution, which avoids the complex regex, is to use the global command to increment a variable on all lines that does NOT match the pattern:

let var=0
v/\v(sss|fff)/let var+=1
echo var

Edit:


You can delete all lines by using the following global command:

g/\v(sss|fff)/d

Upvotes: 3

Susan Eraly
Susan Eraly

Reputation: 339

You can invoke the powers of the command line from vim with %. So type in the following and press enter. It will take you outside vim to give you the answer. Press enter again and you are back in vim exactly as before.

:%!grep -c -v -P "fff|sss"

Upvotes: 2

Related Questions