tags:

views:

298

answers:

4
+2  Q: 

Grep a tab in UNIX

How to grep tab (\t) in the file on Unix platform

A: 
grep '\t' file

works for me, where \t stands for pressing the Tab key, the single quotes prevents it from being discarded as whitespace by the shell.

rsp
which shell? I am using ksh
Sachin Chourasiya
yes even I am also surprised, but frankly speaking its not providing any output for me
Sachin Chourasiya
This doesn't work. It searches for `t`!
Jason Orendorff
my bad, added explanation :-)
rsp
+4  A: 

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

$ grep -P '\t' *
unwind
It doesn't seem to work against my pattern. Attempting to use that syntax prints nothing. (Is the Mac OS X variant different?)
futureelite7
@futureelite: According to Apple's docs (http://developer.apple.com/Mac/library/documentation/Darwin/Reference/ManPages/man1/grep.1.html), the Mac OS X grep program should support the -P option. Consider creating a new question, on superuser.com.
unwind
+2  A: 

One way is (this is with bash)

grep -P '\t'

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

Edit: beaten to it :), but as 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.

tjmoore
Unknown P option in ksh shell
Sachin Chourasiya
As unwind says, may be specific to GNU grep. Just clarified.
tjmoore
A: 

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
what is NF here?
Sachin Chourasiya
number of fields. please read gawk doc to understand more.