Does "\t" work with grep?

I'm trying to grep a simple file (test.txt) for all lines with a tab character in them. This command doesn't return any results:

grep \t test.txt

I've tried enclosing the expression in single quotes and double quotes. What am I doing wrong?

(Yes, I have checked that there are tab characters in the file.)

Mac OS X (10.3.9)

Posted on Dec 4, 2006 2:47 PM

Reply
9 replies

Dec 4, 2006 3:55 PM in response to Adam Davis4

Adam
I've tried enclosing the expression in single quotes
That should work (does for me). I just created a text file using TextEdit:

ibook:~/Desktop michaelc$ od -c tabs.txt
0000000 a \t t a b \n b \t t a b \n c \t t a
0000020 b \n
ibook:~/Desktop michaelc$ grep '\t' tabs.txt
a tab
b tab
c tab

In addition, grep \\t tabs.txt works. You just need to escape the backslash, which can be done with single quotes or another '\'.

Dec 4, 2006 4:41 PM in response to Adam Davis4

Hi Adam, Michael,
Michael, did you install a version of grep, say with Fink? It doesn't work for me either. Also neither "man grep" nor "man re_format" mention escape characters. Try /usr/bin/grep and see if you get the same result. Also, if I could convince you to use zsh, you could use "where grep" to see all versions of grep in your path. With bash, "which grep" would at least tell you the location of the first one found.

Adam, is there a problem with using a literal tab character? You have to quote the tab but you can enter it in the command line with the key sequence <Control>-v,<Tab>. The tab character is special in most shells, initiating command completion, so you must escape it's special meaning with the <Control>-v.
--
Gary
~~~~
'Naomi, sex at noon taxes.' I moan.
Never odd or even.
A man, a plan, a canal, Panama.
Madam, I'm Adam.
Sit on a potato pan, Otis.
Sit on Otis.
-- The Mad Palindromist

Dec 4, 2006 4:59 PM in response to Gary Kerbaugh

Gary
Michael, did you install a version of grep, say with Fink?
Fink? What's that? I didn't take nuffink, guv 🙂

ibook:~ michaelc$ ls -l `which grep`
-rwxr-xr-x 3 root wheel 106056 Mar 20 2005 /usr/bin/grep

Quite frankly I was astonished it worked, fully expecting to have to use the key sequence you quoted.

I don't think I've ever managed to get these backslash sequences to work before, so I have no explanation.

You can see the file from my od -c command (yes, I know hexdump is preferred these days …). I produced it in Textedit (New, then Format > Make Plain Text) using the Tab key, as one would.
Also, if I could convince you to use zsh, …
You don't ever give up, do you 🙂

Dec 4, 2006 7:03 PM in response to Michael Conniff

Hi Michael,
That's really strange. Is your iBook a Pentium or PowerPC Mac? I can't think of any other difference. Here's what I get:

[kerbaugh@kerbaugh]~ 1413 % ls -l `which grep`
-rwxr-xr-x 3 root wheel 106056 Mar 20 2005 /usr/bin/grep
[kerbaugh@kerbaugh]~ 1414 % /usr/bin/grep --version
grep (GNU grep) 2.5.1
...
[kerbaugh@kerbaugh]~ 1415 % echo 'Hi you' | od -c
0000000 H i \t y o u \n
0000007
[kerbaugh@kerbaugh]~ 1416 % echo 'Hi you' | /usr/bin/grep '\t'
[1][kerbaugh@kerbaugh]~ 1417 % echo $?
1
[kerbaugh@kerbaugh]~ 1418 % echo 'Hi there' | /usr/bin/grep '\t'
Hi there

The last command shows what is actually being matched, the 't' in "there". I tried it on a file and in bash. The file was a real file though and had lots of 't's in it so I'm not posting it. Does your grep man page have the following in it?

-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. -P, --perl-regexp Interpret PATTERN as a Perl regular expression.


I swear that mine discusses the -P option inside of the explanation of the -F option. Unfortunately, the -P option isn't implemented. I used Fink to install libpcre but naturally Apple's grep wasn't linked to it. I don't know if that's what would be required to enable the option but Fink's grep is requires libpcre, even in the stable branch. Does yours support the -P option? I don't see how it could and be the same size as mine.
--
Gary
~~~~
-- THE ABC'S OF UNIX
%
better !pout !cry
better watchout
lpr why
santa claus < north pole > town

cat /etc/passwd > list
ncheck list
ncheck list
cat list | grep naughty > nogiftlist
cat list | grep nice > giftlist
santa claus < north pole > town

who | grep sleeping
who | grep awake
who | grep bad || good
for (goodness sake) {
be good
}
%

Dec 4, 2006 7:53 PM in response to Adam Davis4

Adam,

As Gary writes, you can include a real TAB code in the search string by <ctrl-V><TAB> (hit ctrl-V and then hit TAB)

grep '<ctrl-V><TAB>' test.txt

Or you can use a special quoting method $'....'

grep $'\t' test.txt

Michael,

ibook:~/Desktop michaelc$ grep '\t' tabs.txt
a tab


Here, as Gary writes, \t is matched with the character 't' in the string 'tab', not with the TAB-code.



PowerMac G4 Mac OS X (10.4.8)

Dec 5, 2006 8:59 AM in response to Jun T.

Both of the methods you explain work at the command line. Thank you for the explanation.

Now for the next stage of the problem. I have a number of expressions I'm searching for which are listed in a text file (terms.txt). How do I format my expressions within the text file? Neither of the solutions at the command line will work.

Here is the command:

grep -f terms.txt test.txt

Dec 5, 2006 6:06 PM in response to Adam Davis4

Hi Adam,
Actually, entering a literal tab into a text file is as easy or difficult as the text editor you're using makes it. Since it rarely has the special meaning in the text editor that it does in the terminal, it's almost always easier to enter a literal tab into a file than it is to enter it into the command line. You don't even need to quote it like you do in the command line to protect it from the shell. In fact you need to not quote it because the quoting characters will be taken literally.
--
Gary
~~~~
When the fog came in on little cat feet last night, it left these
little muddy paw prints on the hood of my car.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Does "\t" work with grep?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.