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?

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"
  • Note that GNU grep knows \t as tab with -P (if enabled). – Stéphane Chazelas Jul 24 at 10:12
  • @StéphaneChazelas Thanks. Added. – Kusalananda Jul 24 at 10:16
  • Note that in a POSIX regexp, while grep '[\t]' is required to match on backslash and t, grep '\t' is unspecified, so implementations are free to match TAB if they like. Some do like ast-open's – Stéphane Chazelas Jul 24 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.