Bash 循环遍历文件末尾

发布于 2025-01-15 12:00:46 字数 716 浏览 1 评论 0原文

我正在编写一个脚本,该脚本将从一个关键字和一个单独文件上的其他关键字列表中查找模式。

File1 包含列表,每行一个单词。 File2 还有另一个列表——我真正想要搜索的列表。

while read LINE; do
  grep -q $LINE file2
  if [ $? -eq 0 ]; then
    echo "Found $LINE in file2."
    grep $LINE file2 | grep example
    if [ $? -eq 0 ]; then
      echo "Keeping $LINE"
    else
      echo "Deleting $LINE"
      sed -i "/$LINE/d" file2
    fi
  else
    echo "Did not find $LINE in file2."
  fi
done < file1

我想要的是从 file1 中取出每个单词并在 file2 中搜索它的每个实例。我想从这些实例中搜索包含单词 example 的所有实例。任何不包含示例的实例,我想删除它们。

我的代码,它从 file1 中获取一个单词并在 file2 中搜索它的实例。一旦找到该实例,循环就会转到 file1 中的下一个单词,此时它应该继续在 file2 中搜索前一个单词;当它完成在 file2 中搜索当前单词时,它应该只移动到下一个 file1 单词。

对如何实现这一目标有任何帮助吗?

I'm working on script that will find a pattern from a keyword and a list of other keywords on a separate file.

File1 has the list, which is a word per line. File2 has another list--the one that I actually want to search.

while read LINE; do
  grep -q $LINE file2
  if [ $? -eq 0 ]; then
    echo "Found $LINE in file2."
    grep $LINE file2 | grep example
    if [ $? -eq 0 ]; then
      echo "Keeping $LINE"
    else
      echo "Deleting $LINE"
      sed -i "/$LINE/d" file2
    fi
  else
    echo "Did not find $LINE in file2."
  fi
done < file1

What I want is to take each word from file1 and search for every instance of it in file2. From those instances, I want to search for all the instances that contain the word example. Any instances that dont contain example, I want to delete them.

My code, it takes a word from file1 and searches for an instance of it in file2. Once it finds that instance, the loop moves on to the next word in file1, when it should continue searching for file2 for the previous word; it should only move on to the next file1 word when it has completed searching file2 for the current word.

Any help on how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

赤濁 2025-01-22 12:00:46

建议使用 awk 脚本,仅扫描每个文件一次。

 awk 'FRN == RN {wordsArr[++wordsCount] = $0}  # read file1 lines into array
      FRN != RN && /example/ {                 # read file2 line matching regExp /example/
        for (i in wordsArr) {             # scan all words in array
           if ($0 ~ wordsArr[i]) {        # if a word matched in current line
              print;                      # print the current line
              next;                       # skip rest of words,read next line
           }
        }
      }' file1 file2

Suggesting awk script, to scan each file only once.

 awk 'FRN == RN {wordsArr[++wordsCount] = $0}  # read file1 lines into array
      FRN != RN && /example/ {                 # read file2 line matching regExp /example/
        for (i in wordsArr) {             # scan all words in array
           if ($0 ~ wordsArr[i]) {        # if a word matched in current line
              print;                      # print the current line
              next;                       # skip rest of words,read next line
           }
        }
      }' file1 file2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文