how to count how many certain strings are in a file?

1 view
Skip to first unread message

Peng Yu

unread,
Nov 19, 2008, 9:22:50 PM11/19/08
to vim_use
Hi,

Suppose I want search how many '>' is in a file. Is there an easy way
to do it in vim?

Thanks,
Peng

Roberto Miura Honji

unread,
Nov 19, 2008, 9:29:33 PM11/19/08
to vim...@googlegroups.com
One way, not right way, is:
%s/>//g

This tell you how many substitution was done. (how many '>' have in your file)

after
:undo

2008/11/20 Peng Yu <Peng...@gmail.com>



--
----------------------------------------------
Roberto Miura Honji
LAS - Laboratório de Administração e Segurança de Sistemas
Engenharia de Computação - 2006
Instituto de Computação - UNICAMP

email: miura...@gmail.com (principal)
email: ra06...@students.ic.unicamp.br
msn:   miura...@msn.com
-------------------------------------------

Tim Chase

unread,
Nov 19, 2008, 9:29:56 PM11/19/08
to vim...@googlegroups.com
> Suppose I want search how many '>' is in a file. Is there an easy way
> to do it in vim?


You can use the "n" flag to :s

:%s/>//gn

which you can learn more about at

:help :s_flags
:help count-items

-tim

Roberto Miura Honji

unread,
Nov 19, 2008, 9:30:30 PM11/19/08
to vim...@googlegroups.com
You can %s/>/>/g
This way, undo is not necessary

2008/11/20 Roberto Miura Honji <miura...@gmail.com>

Tim Chase

unread,
Nov 19, 2008, 10:34:05 PM11/19/08
to vim...@googlegroups.com
> You can %s/>/>/g
> This way, undo is not necessary

For more complex ones, this can be written

:%s/regexp/&/g

However, even though you know this doesn't change anything, Vim
still things that the content has been modified, and sets the
'modified' flag on the file.

Using the "n" flag

%s/foo//gn

doesn't make the file modified.

-tim


yosi izaq

unread,
Nov 23, 2008, 8:37:49 AM11/23/08
to vim...@googlegroups.com
Some more counting examples:

:%s/./&/gn characters
:%s/\i\+/&/gn words
:%s/^//n lines
:%s/the/&/gn "the" anywhere
:%s/\<the\>/&/gn "the" as a word

Yosi
Reply all
Reply to author
Forward
0 new messages