grep a tab in UNIX

UnixGrep

Unix Problem Overview


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

Unix Solutions


Solution 1 - Unix

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

grep -P '\t' *

Solution 2 - Unix

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

grep $'\t' sample.txt

Solution 3 - Unix

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 - Unix

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" > Use the literal tab character: >
> grep "^V" > Use printf to print a tab character for you: >
> grep "$(printf '\t')"

Solution 5 - Unix

One way is (this is with Bash)

grep -P '\t'

-P turns on Perl regular expressions so \t will work.

As user unwind says, it may be specific to GNU grep. The alternative is to literally insert a tab in there if the shell, editor or terminal will allow it.

Solution 6 - Unix

Another way of inserting the tab literally inside the expression is using the lesser-known $'\t' quotation in Bash:

grep $'foo\tbar'        # matches eg. 'foo<tab>bar'

(Note that if you're matching for fixed strings you can use this with -F mode.)

Sometimes using variables can make the notation a bit more readable and manageable:

tab=$'\t'               # `tab=$(printf '\t')` in POSIX
id='[[:digit:]]\+'
name='[[:alpha:]_][[:alnum:]_-]*'
grep "$name$tab$id"     # matches eg. `bob2<tab>323`

Solution 7 - Unix

Use echo to insert the tab for you grep "$(echo -e \\t)"

Solution 8 - Unix

There are basically two ways to address it:

  1. (Recommended) Use regular expression syntax supported by grep(1). Modern grep(1) supports two forms of POSIX 1003.2 regex syntax: basic (obsolete) REs, and modern REs. Syntax is described in details on re_format(7) and regex(7) man pages which are part of BSD and Linux systems respectively. The GNU grep(1) also supports Perl-compatible REs as provided by the pcre(3) library.

In regex language the tab symbol is usually encoded by \t atom. The atom is supported by BSD extended regular expressions (egrep, grep -E on BSD compatible system), as well as Perl-compatible REs (pcregrep, GNU grep -P).

Both basic regular expressions and Linux extended REs apparently have no support for the \t. Please consult UNIX utility man page to know which regex language it supports (hence the difference between sed(1), awk(1), and pcregrep(1) regular expressions).

Therefore, on Linux:

    $ grep -P '\t' FILE ...

On BSD alike system:

    $ egrep '\t' FILE ...
    $ grep -E '\t' FILE ...

2. Pass the tab character into pattern. This is straightforward when you edit a script file:

    # no tabs for Python please!
    grep -q '	' *.py && exit 1

However, when working in an interactive shell you may need to rely on shell and terminal capabilities to type the proper symbol into the line. On most terminals this can be done through Ctrl+V key combination which instructs terminal to treat the next input character literally (the V is for "verbatim"):

    $ grep '<Ctrl>+<V><TAB>' FILE ...

Some shells may offer advanced support for command typesetting. Such, in bash(1) words of the form $'string' are treated specially:

    bash$ grep $'\t' FILE ...

Please note though, while being nice in a command line this may produce compatibility issues when the script will be moved to another platform. Also, be careful with quotes when using the specials, please consult bash(1) for details.

For Bourne shell (and not only) the same behaviour may be emulated using command substitution augmented by printf(1) to construct proper regex:

    $ grep "`printf '\t'`" FILE ...

Solution 9 - Unix

grep "$(printf '\t')" worked for me on Mac OS X

Solution 10 - Unix

A good choice is to use sed.

sed -n '/\t/p' file

Examples (works in bash, sh, ksh, csh,..):

[~]$ cat testfile
12 3
1 4 abc
xa      c
        a       c\2
1 23

[~]$ sed -n '/\t/p' testfile 
xa      c
        a       c\2
[~]$ sed -n '/\ta\t/p' testfile
        a       c\2

(This answer has been edited following suggestions in comments. Thank you all)

Solution 11 - Unix

use gawk, set the field delimiter to tab (\t) and check for number of fields. If more than 1, then there is/are tabs

awk -F"\t" 'NF>1' file

Solution 12 - Unix

+1 way, that works in ksh, dash, etc: use printf to insert TAB:

grep "$(printf 'BEGIN\tEND')" testfile.txt

Solution 13 - Unix

On ksh I used

grep "[^I]" testfile

Solution 14 - Unix

The answer is simpler. Write your grep and within the quote type the tab key, it works well at least in ksh

grep "	" *

Solution 15 - Unix

This works well for AIX. I am searching for lines containing JOINED<\t>ACTIVE

voradmin cluster status | grep  JOINED$'\t'ACTIVE

 vorudb201   1       MEMBER(g) JOINED        ACTIVE
*vorucaf01   2       SECONDARY JOINED        ACTIVE

Solution 16 - Unix

Using the 'sed-as-grep' method, but replacing the tabs with a visible character of personal preference is my favourite method, as it clearly shows both which files contain the requested info, and also where it is placed within lines:

sed -n 's/\t/\*\*\*\*/g' file_name

If you wish to make use of line/file info, or other grep options, but also want to see the visible replacement for the tab character, you can achieve this by

grep -[options] -P '\t' file_name | sed 's/\t/\*\*\*\*/g'

As an example:

$ echo "A\tB\nfoo\tbar" > test
$ grep -inH -P '\t' test | sed 's/\t/\*\*\*\*/g'
test:1:A****B
test:2:foo****bar

EDIT: Obviously the above is only useful for viewing file contents to locate tabs --- if the objective is to handle tabs as part of a larger scripting session, this doesn't serve any useful purpose.

Solution 17 - Unix

You might want to use grep "$(echo -e '\t')"

Only requirement is echo to be capable of interpretation of backslash escapes.

Solution 18 - Unix

These alternative binary identification methods are totally functional. And, I really like the one's using awk, as I couldn't quite remember the syntaxic use with single binary chars. However, it should also be possible to assign a shell variable a value in a POSIX portable fashion (i.e. TAB=echo "@" | tr "\100" "\011"), and then employ it from there everywhere, in a POSIX portable fashion; as well (i.e grep "$TAB" filename). While this solution works well with TAB, it will also work well other binary chars, when another desired binary value is used in the assignment (instead of the value for the TAB character to 'tr').

Solution 19 - Unix

The $'\t' notation given in other answers is shell-specific -- it seems to work in bash and zsh but is not universal.

NOTE: The following is for the fish shell and does not work in bash:

In the fish shell, one can use an unquoted \t, for example:

grep \t foo.txt

Or one can use the hex or unicode notations e.g.:

grep \X09 foo.txt
grep \U0009 foo.txt

(these notations are useful for more esoteric characters)

Since these values must be unquoted, one can combine quoted and unquoted values by concatenation:

grep "foo"\t"bar"

Solution 20 - Unix

You can also use a Perl one-liner instead of grep resp. grep -P:

perl -ne 'print if /\t/' FILENAME

Solution 21 - Unix

You can type

grep \t foo

or
grep '\t' foo
to search for the tab character in the file foo. You can probably also do other escape codes, though I've only tested \n. Although it's rather time-consuming, and unclear why you would want to, in zsh you can also type the tab character, back to the begin, grep and enclose the tab with quotes.

Solution 22 - Unix

Look for blank spaces many times [[:space:]]*

grep [[:space:]]*'.''.'

Will find something like this:

'the tab' ..

These are single quotations ('), and not double (").
This is how you make concatenation in grep. =-)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSachin ChourasiyaView Question on Stackoverflow
Solution 1 - UnixunwindView Answer on Stackoverflow
Solution 2 - UnixantimirovView Answer on Stackoverflow
Solution 3 - UnixSamKView Answer on Stackoverflow
Solution 4 - UnixPooView Answer on Stackoverflow
Solution 5 - UnixtjmooreView Answer on Stackoverflow
Solution 6 - UnixAlois MahdalView Answer on Stackoverflow
Solution 7 - UnixvanjoeView Answer on Stackoverflow
Solution 8 - UnixMike VolokhovView Answer on Stackoverflow
Solution 9 - Unixkumar303View Answer on Stackoverflow
Solution 10 - UnixJulioView Answer on Stackoverflow
Solution 11 - Unixghostdog74View Answer on Stackoverflow
Solution 12 - UnixZsigmond LőrinczyView Answer on Stackoverflow
Solution 13 - UnixAIXrootView Answer on Stackoverflow
Solution 14 - UnixYullyBearView Answer on Stackoverflow
Solution 15 - UnixgruicView Answer on Stackoverflow
Solution 16 - UnixSilasvbView Answer on Stackoverflow
Solution 17 - UnixkshpolvindView Answer on Stackoverflow
Solution 18 - UnixodoncaoaView Answer on Stackoverflow
Solution 19 - UnixRamanView Answer on Stackoverflow
Solution 20 - UnixGuido FlohrView Answer on Stackoverflow
Solution 21 - UnixAccidental brineView Answer on Stackoverflow
Solution 22 - UnixCaio ArgoloView Answer on Stackoverflow