Home > other >  Bash Loop Through End of File
Bash Loop Through End of File

Time:03-21

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?

CodePudding user response:

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
  • Related