如何输入“串\tstring”?

时间:2020-11-27 19:19:30

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 ...

我需要搜索大量的数据文件。我想找到包含字符串“foo\tbar\tboo”的文件。我试过这个……

$ 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.

…我没有输出。我也试过白鹭。我试过用\t转义\t,但仍然没有输出。

What am I doing wrong?

我做错了什么?

Thanks

谢谢

2 个解决方案

#1


2  

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.

用单引号而不是双引号,并加上一个额外的反斜杠。“阻止bash处理反斜杠,这样grep将实际看到foo\ tbar。基于您的输出,我认为您正在寻找的是文字文本反斜杠,而不是ASCII字符9,因此,将反斜杠加倍以使grep匹配为文字文本。

#2


1  

There are two effects at play here:

这里有两个影响:

  1. grep understands that \t means a tab character.
  2. grep知道\t意味着制表符。
  3. The shell will expand \\ to \ within a double-quoted string.
  4. shell将在双引号字符串中展开\到\。

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

您希望斜杠被转义,所以您需要在单引号中将\t传递给grep:

grep 'foo\\tbar'

#1


2  

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.

用单引号而不是双引号,并加上一个额外的反斜杠。“阻止bash处理反斜杠,这样grep将实际看到foo\ tbar。基于您的输出,我认为您正在寻找的是文字文本反斜杠,而不是ASCII字符9,因此,将反斜杠加倍以使grep匹配为文字文本。

#2


1  

There are two effects at play here:

这里有两个影响:

  1. grep understands that \t means a tab character.
  2. grep知道\t意味着制表符。
  3. The shell will expand \\ to \ within a double-quoted string.
  4. shell将在双引号字符串中展开\到\。

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

您希望斜杠被转义,所以您需要在单引号中将\t传递给grep:

grep 'foo\\tbar'