Jump to content

Grepping for tabs


neddie
 Share

Recommended Posts

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

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:

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

In this case the " and ' don't matter, and the -e has no effect either:

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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...