Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When I search for tabs in a file with (e)grep I use the litteral tab (^v + <tab>). I can not utilize \t as a replacement for tabs in regular expressions. With e.g. sed this expression works very well.

So is there any possibility to use a non-litteral replacement for <tab> and what are the backgrounds for a non working / not interpreted \t ?

share|improve this question

3 Answers 3

up vote 51 down vote accepted

grep is using regular expressions as defined by POSIX. For whatever reasons POSIX have not defined \t as tab.

You have several alternatives:

  • tell grep to use the regular expressions as defined by perl (perl has \t as tab):

    grep -P "\t" foo.txt
    
  • use echo to print a tab character for you:

    grep "$(echo -ne \\t)" foo.txt
    
  • or, as you have already mentioned, use the literal tab character:

    grep "^V<tab>" foo.txt
    

    that is: type grep ", then press ctrl+v, then press tab, then type " foo.txt. pressing ctrl+v in the shell causes the next key to be taken literally. that means the shell will insert a tab character instead of triggering some function bound to the tab key.

  • if you use bash you can use the ansi c quoting feature:

    grep $'\t' foo.txt
    

See the wikipedia article about regular expressions for an overview of the defined character classes in POSIX and other systems.

share|improve this answer
    
Thank you for your exact explanations. –  Lasall Jul 14 '11 at 15:13
    
basing on enzotib's answer let me add the following: grep $'\t' foo.txt (but I would usually write fgrep instead of grep) –  Walter Tross Feb 21 '13 at 10:16

It is not exactly the answer you would to ear, but a possible use of escape sequences is provided by bash

command | grep $'\t'

(do not put it into double quotes!).

share|improve this answer
    
Thank you for publish your suggestion. Even it does not make use of greps own mechanisms it is a smart solution. –  Lasall Jul 14 '11 at 15:13
    
there is no need for the -E (what is searched for is no regex). There is also no need to pipe from a command. That said, thank you for pointing out this quite overlooked feature of bash (single-quoted strings preceded by $) –  Walter Tross Feb 21 '13 at 10:29
    
Indeed, I suggest that @enzotib edit the answer to be simply grep $'\t'. –  Teemu Leisti Jun 4 '13 at 12:01

regex defines that [[:space:]] searches for both spaces and tabs. u can also try:

egrep "[:blank:]{2,5}

this will find most of the tabs (since mostly a tab is 2-5 spaces long.

using \t in sed is probably an enhancement of sed itselves.

share|improve this answer
    
A tab isn't any number of spaces long, it is an entirely separate character. It just looks like a number of spaces when it is shown by a text editor. –  OrbWeaver Jul 14 '11 at 14:45
2  
Thank you for taking the time to answer this question. But the character class [:blank:] includes spaces and tabs. So your solution unfortunately does not distinguish between them. –  Lasall Jul 14 '11 at 15:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.