0

I'm looking for all tabs between two quotation marks with grep / egrep in a bash.

I tried

grep -r . -e "\".*\t.*\""
grep -r . -e "\".*\\t.*\""
grep -r . -e "\".*\\\\t.*\""
grep -r . -e '\".*\t.*\"'
grep -r . -e '\".*\\t.*\"'
grep -r . -e '".*\t.*"'
grep -r . -e '".*\\t.*"'

But I don't get the desired result. Following greps yield all line with a \t:

grep -r . -e "\".*\\\\t.*\""
grep -r . -e '\".*\\t.*\"'
grep -r . -e '".*\\t.*"'

Following greps yield all line with a t:

grep -r . -e "\".*\t.*\""
grep -r . -e "\".*\\t.*\""
grep -r . -e '".*\t.*"'
grep -r . -e '\".*\t.*\"'

But none of these searches for a real tabulator. What I'm doing wrong?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Thomas Sablik
  • 132
  • 1
  • 8

1 Answers1

2

GNU grep (which I assume you are using), does not know \t as a tab by default. In a POSIX regular expression, \t would match a literal t, and \\t would match \t.

To match a literal tab character with a POSIX regular expression, insert one in your expression:

grep -r $'".*\t.*"' .

This uses $'...' in bash to expand the escape sequences in the string to their corresponding meaning in the C language.

If you enable Perl-like regular expressions (PCRE) with -P in GNU grep, it will recognise \t as a tab character in the expression:

grep -P -r '".*\t.*"' .

Note too that your expression would match

"hello" <tab> "world"
Kusalananda
  • 320,670
  • 36
  • 633
  • 936