Red Cricket
Red Cricket

Reputation: 10470

How to grep for "string\tstring"?

I need to search a large group of data files. I want to find files that contain the string "foo\tbar\tboo". I have tried this ...

$ find . -name "foo*dat" -exec grep foo {} \; | less
          "miscinfo_foo" => [
                               "foo\tbar\tnot_foo"
                                      "miscinfo_foo",
          "miscinfo_foo" => [
                               "foo\tbar\tyes_foo"
                                      "miscinfo_foo",

But if I do ...

$ find . -name "foo*dat" -exec grep -E "foo\tbar" {} \;

... I get no output. I have tried egrep too. I have tried escaping the \t with \\t but still get no output.

What am I doing wrong?

Thanks

Upvotes: 2

Views: 112

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74685

There are two effects at play here:

  1. grep understands that \t means a tab character.
  2. The shell will expand \\ to \ within a double-quoted string.

You want the slash to be escaped, so you need to pass \\t to grep within single quotes:

grep 'foo\\tbar'

Upvotes: 1

cxw
cxw

Reputation: 17051

Try

find . -name "foo*dat" -exec grep -E 'foo\\tbar' {} \;
                                     ^   ^     ^

in single quotes rather than double, and with an extra backslash. The '' prevent bash from processing backslashes, so that grep will actually see foo\\tbar. Based on your output, I think you are looking for the literal text backslash-tee, not an ASCII character 9, so double the backslash to have grep match it as literal text.

Upvotes: 2

Related Questions