linux - 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
解决方案
您可以使用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)
推荐阅读
- rust - 使用 Rust 构建 `.wasm` 二进制文件时访问 `__heap_base`
- vb.net - 如何使用 vb.net 从字符串中消除多余元素
- python - 绘制 PyqtGraph 字符串和浮点数
- java - 如何在构建后脚本中访问调用程序集成测试的控制台输出?
- git - 更改推送建议的默认遥控器
- unit-testing - vstest.console 进程在 90 秒后无法连接到 testhost 进程
- r - 数据框中从长格式到宽格式
- python - Python:将列表添加到数组
- annotations - 将 Yolov5 bbox 导出为 Pascal 格式
- excel - 在 Excel VBA 宏中组合两种类型的粘贴