grep a tab in UNIX

How do I grep tab (\t) in files on the Unix platform?


Solution 1:

If using GNU grep, you can use the Perl-style regexp:

grep -P '\t' *

Solution 2:

The trick is to use $ sign before single quotes. It also works for cut and other tools.

grep $'\t' sample.txt

Solution 3:

I never managed to make the '\t' metacharacter work with grep. However I found two alternate solutions:

  1. Using <Ctrl-V> <TAB> (hitting Ctrl-V then typing tab)
  2. Using awk: foo | awk '/\t/'

Solution 4:

From this answer on Ask Ubuntu:

Tell grep to use the regular expressions as defined by Perl (Perl has \t as tab):

grep -P "\t" <file name>

Use the literal tab character:

grep "^V<tab>" <filename>

Use printf to print a tab character for you:

grep "$(printf '\t')" <filename>