我构建了一个grep命令,用于递归搜索文件目录中的模式。问题是grep只返回模式所在的文件名,而不是模式的完全匹配。如何返回实际结果?
示例:
文件somefile.bin
在包含一百万个其他文件的目录中包含somestring0987654321�123�45�
命令:
$ grep -EsniR -A 1 -B 1 '([a-zA-Z0-9]+)\x00([0-9]+)\x00([0-9]+)\x00' *
目前的结果:
Binary file somefile.bin matches
期望的结果(或接近它):
Binary file somefile.bin matches
<line above match>
somestring0987654321�123�45�
<line below match>
答案 0 :(得分:1)
您可以尝试-a
选项:
File and Directory Selection
-a, --text
Process a binary file as if it were text; this is equivalent to
the --binary-files=text option.
--binary-files=TYPE
If the first few bytes of a file indicate that the file contains
binary data, assume that the file is of type TYPE. By default,
TYPE is binary, and grep normally outputs either a one-line
message saying that a binary file matches, or no message if
there is no match. If TYPE is without-match, grep assumes that
a binary file does not match; this is equivalent to the -I
option. If TYPE is text, grep processes a binary file as if it
were text; this is equivalent to the -a option. Warning: grep
--binary-files=text might output binary garbage, which can have
nasty side effects if the output is a terminal and if the
terminal driver interprets some of it as commands.
但问题是在二进制文件中没有行,所以我不确定你想要的输出是什么样的。你会看到随机垃圾,也许是整个文件,可能会打印一些与你的终端混乱的特殊字符。
如果要将输出限制为匹配本身,请考虑-o
选项:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
上下文控制仅限于在匹配之前或之后添加一定数量的行,这在这里可能效果不佳。因此,如果您需要特定字节数的上下文,则必须更改模式本身。
答案 1 :(得分:0)
尝试...
grep -rnw "<regex>" <folder>
容易得多。这里有更多示例-> https://computingbro.com/2020/05/10/word-search-in-linux-unix-filesystem/