【问题标题】:How to continue in loop on first GREP result如何在第一个 GREP 结果上继续循环
【发布时间】:2015-12-05 13:09:46
【问题描述】:

我有以下 .sh 文件,它在大量文件中搜索大量搜索项。但是如果 $file 中存在一个搜索项,我想在第一个结果时继续 while 循环。目前,这是为了在所有文件中匹配一个查询。第一击就够了。

我该怎么做?

while read file
do
    echo $file
    grep -o -f searchItems.txt "$file" >> results.txt
done < filelist.txt

谢谢。

【问题讨论】:

    标签: shell loops while-loop grep do-while


    【解决方案1】:

    grep成功后可以使用break返回:

    while read -r key; do
       while read -r actualFile; do
         echo "searching for $key in $actualFile"
         grep -o "$key" "$actualFile" >> messageKeysInUse.txt && break
       done < filelist.txt
    done < allMessageKeysFromDB.txt
    

    只要grep 成功,它就会跳出内部while循环。

    【讨论】:

    • 抱歉,它不起作用。 31 秒后我只有 4 次命中。我预计大约 900 个。
    • 谢谢。但我认为我必须切换 while 循环,因为我的 results.txt 有很多相等的命中。
    • 我正在任何文件中搜索密钥。如果任何文件中存在密钥,我想在所有文件中搜索下一个密钥,直到我第一次找到它。
    • 今天早上脚本还在运行,我在 results.txt 中看到了大约 20 个具有相同单词的条目。
    • 好的更新答案。所以现在这对你不起作用吗?当grep 成功时,内部while 将中断,而外部while 将迭代到下一个键。
    猜你喜欢
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多