在文件夹中查找文本的最快方法
我使用 fgrep -s 'text' /folder/*.txt
的返回值来查找 'text' 是否在 any 中 < em>.txt 文件位于 /folder/ 中。它可以工作,但我发现它对于我需要的东西来说太慢了,就像它在给我答案之前在所有文件中搜索'text'一样。
我需要一种东西,当它找到至少一个带有“文本”的文件时,它能快速给出是/否答案。可能是一些 awk 脚本。
I was using the return value of fgrep -s 'text' /folder/*.txt
to find if 'text' is in any .txt file in /folder/. It works, but I find it too slow for what I need, like if it searches for 'text' in all the files before giving me an answer.
I need something that quickly gives me a yes/no answer when it finds at least one file with the 'text'. Probably some awk script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解你的问题,你想要:
一场比赛后停止。
If I understand your question correctly, you want:
Which stops after one match.
如果是基于 mopoke 答案的搜索,您可以使用它来缩短搜索时间。这会在找到它的第一个文件中的第一个匹配项之后停止:
编辑:注释掉一些行并进行一些其他更改。
如果有大量文件,则
for
可能会失败,您应该使用while
循环,并通过管道将find
插入其中。如果您想要做的只是找出文件夹中是否有任何 .txt 文件,而不管该文件的内容如何,那么请单独使用如下内容:
You can use this to shorten your search if it's the kind that would be based on mopoke's answer. This stops after the first match in the first file in which it's found:
Edit: commented out some lines and made a couple of other changes.
If there is a large number of files, then the
for
could fail and you should use awhile
loop with afind
piped into it.If all you want to do is find out whether there is any .txt file in a folder regardless of the file's contents, then use something like this all by itself:
根据上面的答案,我认为这应该可以吗?
但我不确定我是否完全理解这个问题:“fgrep”在开始输出之前是否进行了完整的深度递归?发现的内容应该按其发现情况进行报告,因此可能更适合您的需要,不知道。
[编辑,未测试]:将上面的 'grep' 更改为执行以下操作的 shell 脚本:
要获得 true|false ,您需要工作(您需要使用这个,尚未完全测试)此处的步骤 - 目前无法访问 Unix :-( )
Building on the answers above, this should do it I think ?
But I'm not sure I fully understand the problem : is 'fgrep' doing a full depth recursion before it starts outputting or something ? The find should report as-it-finds so might be better for what you need, dunno.
[edit, not tested]: Change the 'grep' above for a shell-script that does something like:
To get the true|false thing you need to work (you'll need to play around with this , haven't tested exactly the steps here - no access to Unix at the moment :-( )