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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...