count number of tab characters in linux

ravi picture ravi · Jun 14, 2012 · Viewed 9.3k times · Source

I want to count the numbers of hard tab characters in my documents in unix shell.

How can I do it?

I tried something like

grep -c \t foo

but it gives counts of t in file foo.

Answer

William Pursell picture William Pursell · Jun 14, 2012

Use tr to discard everything except tabs, and then count:

< input-file tr -dc \\t | wc -c