views:

247

answers:

2

How can you count the number of the character < in your files?

I am trying to count the number of the character "<" in my files, similarly as in Vim by

%s/<//gn

I run

 find * -type f | xargs sed 's/<//gn'

I get

sed: -e expression #1, char 7: unknown option to `s'

This error suggests me that Vim's :s -mode is not like SED.

+2  A: 

I'm not familiar with the n option in Vim, but yes, sed does not support the n option. Here's another way you could count the number of instances of the < character in your files:

find * -type f | xargs cat | tr -d -c '<' | wc -c

The tr -d -c '<' command deletes all characters that are not <, and then wc -c counts the number of remaining characters.

Adam Rosenfield
**Does this `find * -type f | xargs cat | tr -d -c '<'` replace nearly everything in my files?**
Masi
No, it does not touch your files -- it prints them out to a pipe with cat, and then the pipe is filtered. The original files are not modified.
Adam Rosenfield
Let's assume that I remove this `cat |`. **How does the command change?**
Masi
Then it doesn't work, because `tr` doesn't accept filenames -- it *only* reads and writes from stdin and stdout respectively.
Adam Rosenfield
A: 
grep -RoE "<" * |wc -l