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,738
  • 21
  • 105
  • 131
anon
  • 103
  • 1
  • 4

2 Answers2

17

Try this instead:

grep -G $'\t'

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

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 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
2

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,629
  • 2
  • 22
  • 28
  • 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
  • this is still experimental option. for example this refuses to use multiline regexp in external file for -f grep option. – Znik Jun 21 '22 at 17:43