ravi
ravi

Reputation: 3424

count number of tab characters in linux

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.

Upvotes: 9

Views: 11486

Answers (6)

glenn jackman
glenn jackman

Reputation: 246877

You can use awk in a tricky way: use tab as the record separator, then the number of tab characters is the total number of records minus 1:

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

Upvotes: 2

chepner
chepner

Reputation: 531355

Bash uses a $'...' notation for specifying special characters:

grep -c $'\t' foo

Upvotes: 11

William Pursell
William Pursell

Reputation: 212278

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

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

Upvotes: 15

LukeShu
LukeShu

Reputation: 75

My first thought was to use sed to strip out all non-tab characters, then use wc to count the number of characters left.

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

However, this also counts newlines, which sed won't touch because it is line-based. So, let's use tr to translate all the newlines into spaces, so it is one line for sed.

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

Depending on your shell and implementation of sed, you may have to use a literal tab instead of \t, however, with Bash and GNU sed, the above works.

Upvotes: 0

Joni
Joni

Reputation: 111289

You can insert a literal TAB character between the quotes with Ctrl+V+TAB.

In general you can insert any character at all by prefixing it with Ctrl+V; even control characters such as Enter or Ctrl+C that the shell would otherwise interpret.

Upvotes: 3

dogbane
dogbane

Reputation: 274640

Use a perl regex (-P option) to grep tab characters.

So, to count the number of tab characters in a file:

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

Upvotes: 4

Related Questions