在文件夹中查找文本的最快方法

发布于 2024-08-15 21:57:00 字数 268 浏览 9 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨夜星沙 2024-08-22 21:57:00

如果我正确理解你的问题,你想要:

fgrep -m1

一场比赛后停止。

If I understand your question correctly, you want:

fgrep -m1

Which stops after one match.

以为你会在 2024-08-22 21:57:00

如果是基于 mopoke 答案的搜索,您可以使用它来缩短搜索时间。这会在找到它的第一个文件中的第一个匹配项之后停止:

# found=$(false)
found=1    # false
text="text"
for file in /folder/*.txt
do
    if fgrep -m1 "$text" "$file" > /dev/null
    then
        found=$?
        # echo "$text found in $file"
        break
    fi
done
# if [[ ! $found ]]
# then
#    echo "$text not found"
# fi
echo $found # or use exit $found

编辑:注释掉一些行并进行一些其他更改。

如果有大量文件,则 for 可能会失败,您应该使用 while 循环,并通过管道将 find 插入其中。

如果您想要做的只是找出文件夹中是否有任何 .txt 文件,而不管该文件的内容如何,​​那么请单独使用如下内容:

find /folder -name "*.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:

# found=$(false)
found=1    # false
text="text"
for file in /folder/*.txt
do
    if fgrep -m1 "$text" "$file" > /dev/null
    then
        found=$?
        # echo "$text found in $file"
        break
    fi
done
# if [[ ! $found ]]
# then
#    echo "$text not found"
# fi
echo $found # or use exit $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 a while loop with a find 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:

find /folder -name "*.txt"
预谋 2024-08-22 21:57:00

根据上面的答案,我认为这应该可以吗?

find /folder -name \*.txt -exec grep "text" {}\;

但我不确定我是否完全理解这个问题:“fgrep”在开始输出之前是否进行了完整的深度递归?发现的内容应该按其发现情况进行报告,因此可能更适合您的需要,不知道。

[编辑,未测试]:将上面的 'grep' 更改为执行以下操作的 shell 脚本:

grep "text" && exit 0 || exit 1

要获得 true|false ,您需要工作(您需要使用这个,尚未完全测试)此处的步骤 - 目前无法访问 Unix :-( )

Building on the answers above, this should do it I think ?

find /folder -name \*.txt -exec grep "text" {}\;

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:

grep "text" && exit 0 || exit 1

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 :-( )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文