10

I used to use grep -P successfully earlier, till I got a machine where grep is not compiled with Perl regular expression support. Now I am having trouble matching tabs: \t character,

grep -G '\t' matches a literal 't'
grep -E '\t' matches a literal 't'

How do I match tabs?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
anon
  • 103
  • 1
  • 4

2 Answers2

16

Try this instead:

grep -G $'\t'

For more info please see bug #23535: Grep Doesn't Support \t as a tab character.

Andrew Hare
  • 333,516
  • 69
  • 632
  • 626
  • Indeed, thanks for the suggestion. Though I didn't need the -G option for solving my problem, only the escaping sequence (in my case, something like this worked: grep $'\t'$patternPrefix$'\t'morePatternData searchFile ). – J.B. Brown Jul 21 '12 at 09:20
1

The Perl-RegEx understand '[\t]', so you can do like this:

echo -e "\t : words" | grep -P '^[\t]'

the basic-regexp and extended-regexp need for these a "blank" (RegEx for "Space" / "Tab"):

echo -e "\t : words" | grep -G '^[[:blank:]]'
echo -e "\t : words" | grep -E '^[[:blank:]]'

maybe you can use it this way:

echo -e "\t : words" | grep -E '^[[:blank:]]{2,8}'
echo -e "\t : words" | grep -G '^[[:blank:]]\{2,8\}'
Martin Jambon
  • 4,428
  • 2
  • 21
  • 26
  • 1
    `grep -P` is great (Perl syntax, fast implementation) but unfortunately it is optional and some distributions still don't include it by default. – Martin Jambon Sep 30 '15 at 21:53