Monday, February 21, 2022

[SOLVED] While loop to break when pattern is found in all files?

Issue

The below code searches for a set of patterns (contained in the $snps variable) within multiple files ($file variable for files ending in snp_search.txt) and outputs a long list of whether or not each snp is in each file.

The purpose is to find several SNPs that are in all of the files.

Is there a way to embed the below code in a while loop so that the it keeps running until it finds a SNP that is in all of the files and breaks when it does? Otherwise I have to check the log file manually.

for snp in $snplist; do
   for file in *snp_search.txt; do

     if grep -wq "$snp" $file; then
       echo "${snp} was found in $file" >> ${date}_snp_search.log; else
       echo "${snp} was NOT found in $file" >> ${date}_snp_search.log
     fi
   done
done

Solution

You can use grep to search all the files. If the file names don't contain newlines, you can just count the number of matching files directly:

#! /bin/bash
files=(*snp_search.txt)
count_files=${#files[@]}
for snp in $snplist ; do
    count=$(grep -wl "$snp" *snp_search.txt | wc -l)
    if ((count == count_files)) ; then
        break
    fi
done

For file names containing newlines, you can output the first matching line for each $snp without the file name and count the lines:

count=$(grep -m1 -hw "$snp" *snp_search.txt | wc -l)


Answered By - choroba
Answer Checked By - Terry (WPSolving Volunteer)