首页 > 解决方案 > While loop to break when pattern is found in all files?

问题描述

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

标签: linuxbashloopsfor-loopwhile-loop

解决方案


您可以使用grep搜索所有文件。如果文件名不包含换行符,您可以直接计算匹配文件的数量:

#! /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

对于包含换行符的文件名,您可以为每个 $snp 输出不带文件名的第一个匹配行并计算行数:

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

推荐阅读