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 ?
4 Answers
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
\tas tab):grep -P "\t" foo.txtthe man page warns that this is an "experimental" feature. at least
\tseems to work fine. but more advanced perl regex features may not.use printf to print a tab character for you:
grep "$(printf '\t')" foo.txtuse the literal tab character:
grep "^V<tab>" foo.txtthat is: type
grep ", then pressctrl+v, then presstab, then type" foo.txt. pressingctrl+vin the terminal causes the next key to be taken verbatim. that means the terminal will insert a tab character instead of triggering some function bound to the tab key.use the ansi c quoting feature of bash:
grep $'\t' foo.txtthis does not work in all shells.
use awk:
awk '/\t/'use sed:
sed -n '/\t/p'
See the wikipedia article about regular expressions for an overview of the defined character classes in POSIX and other systems.
3It is not exactly the answer you would want to hear, but a possible use of escape sequences is provided by bash
command | grep $'\t'(do not put it into double quotes!).
6awk '/\t/' is my favorite workaround:
printf 'a\t\nb' | awk '/\t/'Output: a\t.
One can always resort to using ascii hex-code for tab:
$ echo "one"$'\t'"two" > input.txt
$ grep -P "\x9" input.txt
one two
$ grep $'\x9' input.txt
one two