Discussion:
Grep for tab character
Robert Mark Bram
2005-02-20 04:55:35 UTC
Permalink
Hi All!

I cannot seem to use grep for tabs. Here is what I have tried..

This shows that there are tabs in the file test.txt:

Robert Mark Bram - /tmp
$cat test.txt | sed s/[\\t]/tab/g
tabdr
tabHow
tabHow
tabHow
tabHow
How
How

And these lines show how I am trying to grep for tab, but with no luck!

Robert Mark Bram - /tmp
$grep -e [\\t]How test.txt

Robert Mark Bram - /tmp
$grep -e [\t]How test.txt

Robert Mark Bram - /tmp
$grep -e "[\t]How" test.txt

Robert Mark Bram - /tmp
$grep -e "[\\t]How" test.txt

Robert Mark Bram - /tmp
$

Am I missig something? Or is it cygwin grep?

Any advice would be most appreciated!

Rob
:)
--
Robert Mark Bram
http://phd.netcomp.monash.edu.au/RobertMarkBram/default.asp
B.Comp.(Systems Development/Business Systems)
B.Net.Comp.(Hons)
Doctor of Philosophy Student

School of Network Computing
Faculty of Information Technology
Monash University
Peninsula Campus
McMahons Rd
Frankston, VIC 3199
AUSTRALIA
Christopher Faylor
2005-02-20 05:13:40 UTC
Permalink
Post by Robert Mark Bram
I cannot seem to use grep for tabs. Here is what I have tried..
And these lines show how I am trying to grep for tab, but with no luck!
Robert Mark Bram - /tmp
$grep -e [\\t]How test.txt
Robert Mark Bram - /tmp
$grep -e [\t]How test.txt
Robert Mark Bram - /tmp
$grep -e "[\t]How" test.txt
Robert Mark Bram - /tmp
$grep -e "[\\t]How" test.txt
Robert Mark Bram - /tmp
$
Am I missig something? Or is it cygwin grep?
How about just using the actual tab character? I don't see any indication that
grep is supposed to treat '\t' specially and it seems to behave that way on linux,
too.

What's wrong with just using tab?

cgf
Robert Mark Bram
2005-02-20 05:46:22 UTC
Permalink
Hi Christopher,

Thank you for your response!
Post by Christopher Faylor
How about just using the actual tab character? I don't see any indication that
grep is supposed to treat '\t' specially and it seems to behave that way on linux,
too.
I have read in many places that \t is a metacharacter for tab in regular
expressions - but maybe that's only for sed, perl, awk etc...
http://sitescooper.org/tao_regexps.html
Post by Christopher Faylor
What's wrong with just using tab?
Because that starts up auto-complete. In other words, I don't know how to
insert a literal tab into a command line.

But I do not like that idea for scripts either - it can be difficult to
visually discriminate a tab from a space character, which can too easily
lead to errors.

Rob
:)
--
Robert Mark Bram
http://phd.netcomp.monash.edu.au/RobertMarkBram/default.asp
B.Comp.(Systems Development/Business Systems)
B.Net.Comp.(Hons)
Doctor of Philosophy Student

School of Network Computing
Faculty of Information Technology
Monash University
Peninsula Campus
McMahons Rd
Frankston, VIC 3199
AUSTRALIA
Brian Dessent
2005-02-20 07:29:48 UTC
Permalink
Post by Robert Mark Bram
Post by Christopher Faylor
How about just using the actual tab character? I don't see any indication that
grep is supposed to treat '\t' specially and it seems to behave that way on linux,
too.
I have read in many places that \t is a metacharacter for tab in regular
expressions - but maybe that's only for sed, perl, awk etc...
http://sitescooper.org/tao_regexps.html
Try "grep -P '\t'" to use perl-compatible regexps. Note that this is a
specific capability of GNU grep, so it will not be portable to systems
that use a different grep. It might be more portable to use "awk
'/\t/'".

Or <ctrl-v><tab> to insert a literal tab as others said.

Brian
fergus
2005-02-20 05:32:09 UTC
Permalink
Try
grep "<Ctrl-v><Ctrl-i>"
i.e. type 4 characters being quotes <Ctrl-v> <Ctrl-i> quotes.
I think that works.
(I'm not certain that just typing <Tab> always (ever?) works, for me
anyway.)
Fergus
Buchbinder, Barry (NIH/NIAID)
2005-02-22 13:30:37 UTC
Permalink
Post by Robert Mark Bram
Because that starts up auto-complete. In other words, I don't know
how to insert a literal tab into a command line.
<control-V><tab>
Post by Robert Mark Bram
But I do not like that idea for scripts either - it can be difficult
to visually discriminate a tab from a space character, which can too
easily lead to errors.
Then try

sed -n '/\t/p' file

Continue reading on narkive:
Loading...