neddie Posted August 17, 2006 Report Share Posted August 17, 2006 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] Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted August 17, 2006 Report Share Posted August 17, 2006 This is bash, right? (Just to clarify) Quote Link to comment Share on other sites More sharing options...
neddie Posted August 17, 2006 Author Report Share Posted August 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted August 17, 2006 Report Share Posted August 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
theYinYeti Posted August 18, 2006 Report Share Posted August 18, 2006 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. Quote Link to comment Share on other sites More sharing options...
neddie Posted August 18, 2006 Author Report Share Posted August 18, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.