Count the number of tab characters in linux

I want to count the numbers hard tab charactersin my documents in a unix shell.

How can i do this?

I tried something like

grep -c \t foo

but it gives the number t in the foo file.

+5
source share
6 answers

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

< input-file tr -dc \\t | wc -c
+12
source

Bash uses a notation $'...'to indicate special characters:

grep -c $'\t' foo
+7
source

TAB Ctrl+V + TAB.

, , Ctrl+V; , Enter Ctrl+C, .

+3

perl regex (-P) grep tab.

, :

grep -o -P '\t' foo | wc -l
+2

awk : , - 1:

ntabs=$(awk 'BEGIN {RS="\t"} END {print NR-1}' foo)
+1

, sed , wc .

< foo.txt sed 's/[^\t]//g' | wc -c

, sed , . , tr , sed.

< foo.txt tr '\n' ' ' | sed 's/[^\t]//g' | wc -c

Depending on your shell and implementation, sedyou may need to use a literal tab instead \t, however with Bash and GNU sed, this works.

0
source

All Articles