Help - Search - Members - Calendar
Full Version: Grepping for tabs
MandrivaUsers.org > General Help > Tips and Tricks
neddie
Here's an odd one, I just banged my head against it so thought I'd post it here.

I was trying to do a grep on a set of files, looking for tabs at the end of lines. So I did as you would probably do, ie
CODE
grep -R "\t$" *

Right? Wrong. For some reason grep just ignores the \ and looks for the letter "t" at the end of lines. Weird, eh?

After some searching I found out the answer is to not use \t but to use Ctrl-v and then the <tab> key to put the tab character directly in the command. Odd. So it looks like:
CODE
grep -R "   $" *
where the tab character is inserted as Ctrl-v and then Tab.

Anyway, maybe it's also useful for other cases where you want to put tabs into commands, can't think of any at the moment but maybe there are some...

[moved from TSC,K&P by tyme]
Steve Scrimpshire
This is bash, right? (Just to clarify)
neddie
Yeah, this is bash. Although I assume grep is grep, so whichever shell you call it from, it still won't understand \t. The Ctrl-v bit might be bash-specific, don't know. But it works on both Mandriva and Sun.
Steve Scrimpshire
QUOTE
The Ctrl-v bit might be bash-specific...


That's what I was thinking. Maybe users of ksh or tcsh can verify if it works there, too.
theYinYeti
Don't forget that "..." strings are parsed by the shell, whereas '...' strings are not.
So assuming \t is known by grep, '\t$' or "\\t$" should work, but "\t$" may indeed not.
However, I'm not sure grep knows about \t, so you might have to use the -e option, or use [[:space:]] instead of \t if it suits you (this would match tabs AND spaces).

Yves.
neddie
In this case the " and ' don't matter, and the -e has no effect either:
CODE
grep "\t$" *            no
grep "\\t$" *           no
grep \t$ *              no
grep \\t$ *             no
grep '\t$' *            no
grep '\\t$' *           no
grep [[:space:]]$   *   yes!
grep '[[:space:]]$' *   yes!
grep "[[:space:]]$" *   yes!

Didn't know about [[:space:]], thanks.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.