简体   繁体   English

使用Ag(“白银搜索者”)查找十六进制代码

[英]Using Ag (“The Silver Searcher”) to find hex codes

I used to use find command for linux to find hex code in php code as the following: 我曾经在Linux下使用find命令在php代码中查找十六进制代码,如下所示:

find "/www_root/myfile" -type f -name "*.php" | xargs grep -il x29

The above command works like a charm, but I may need a faster searcher as my files are keep growing. 上面的命令就像一个超级按钮,但是随着我的文件不断增长,我可能需要一个更快的搜索器。

I would like to test using The Silver Searcher ag Command, and I have been searching around of how to find hex code using "ag" command, but cannot find any in their documentation. 我想使用The Silver Searcher ag命令进行测试,并且一直在寻找如何使用“ ag”命令查找十六进制代码的方法,但是在其文档中找不到任何内容。 So, here I am in this forum, seeking for answer in the hope someone has experienced on searching hex code using the silver searcher ag . 所以,我在这个论坛上,寻求答案,希望有人在使用silver searcher ag搜索十六进制代码方面经验丰富。

Somehow, I did not found this a bit earlier, but now has been found. 不知何故,我没有早一点找到它,但是现在已经找到了。 What I did not understand was actually the "ag" command and functions that could be used but now it's ok. 我不明白的是实际上可以使用的“ ag”命令和功能,但是现在还可以。

I will share the answer here just in case someone might need it. 我将在这里分享答案,以防万一有人需要它。 In order to replace the find command as I questioned previously, we can use this: 为了替换我之前询问的find命令,我们可以使用以下命令:

ag --php -l 'x29' "/www_root/myfile"

Output of the previous command would be something like: 上一条命令的输出类似于:

/www_root/myfile/menus.php

If it found more files, then it will list files separated by new line "\\n": 如果找到更多文件,它将列出用新行“ \\ n”分隔的文件:

/www_root/myfile/menus.php
/www_root/myfile/contents.php

If you would like to know which line numbers, you can then drop the "-l" attribute and then replace it with "--numbers": 如果您想知道哪些行号,则可以删除“ -l”属性,然后将其替换为“ --numbers”:

ag --php --numbers 'x29' "/www_root/myfile"

Output: 输出:

30:matches string found here
89:matches string found here

Ok, now we can combine it with "cut" command to get only the line numbers: 好的,现在我们可以将其与“ cut”命令结合使用以仅获取行号:

ag --php --numbers 'x29' "/www_root/myfile" | cut -f1 -d:

Output: 输出:

30
89

Most of the commands are similar to "ack", so for you who wants to use "ag" can find "ack" documentation as they have documented everything. 大多数命令类似于“ ack”,因此对于想要使用“ ag”的人来说,可以找到“ ack”文档,因为它们已经记录了所有内容。

Last but not least, if you would like to know the statistic, you can add --stats to the above command as the following: 最后但并非最不重要的一点是,如果您想了解统计信息,可以将--stats添加到上述命令中,如下所示:

ag --php -l --stats 'x29' "/www_root/myfile"

That simple command will output as the result shown: 该简单命令将输出,如下所示:

31 matches
1624 files searched
18686162 bytes searched
0.094022 seconds

So, yes, choosing The Silver Searcher was the right choice. 因此,是的,选择Silver Searcher是正确的选择。 That's all and have a great day. 仅此而已,祝您拥有美好的一天。

Also have a look at findrepo which you could use like: 还可以看看findrepo ,您可以像这样使用:

cd /www_root/myfile && findrepo -n 'x29' '*.php'

That should be faster than ag (and I'm surprised you said that your initial find | xargs grep command was slower than ag ) 那应该比ag快(让我惊讶的是,您说您的最初find | xargs grep命令比ag慢)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM