我正在尝试编写一个脚本,该脚本使用agrep循环遍历一个文档中的文件,并将它们与另一个文档进行匹配。我相信这可能会使用嵌套循环,但是,我不能完全确定。在模板文档中,我需要它获取一个字符串并将其与另一个文档中的其他字符串匹配,然后移动到下一个字符串并再次匹配它。

如果由于一些奇怪的原因无法看到图片,我已经包括在底部的链接,以及这里。如果你需要我解释更多的话,请告诉我。这是我的第一篇文章,所以我不确定这将如何被理解,或者我是否使用了正确的术语:)
Template agrep/highlighted- https://imgur.com/kJvySbW
Matching strings not highlighted- https://imgur.com/NHBlB2R我已经看过关于循环的各种网站了。
#!/bin/bash
#agrep script
echo ${BASH_VERSION}
TemplateSpacers="/Users/kj/Documents/Research/Dr. Gage
Research/Thesis/FastA files for AGREP
test/Template/TA21_spacers.fasta"
MatchingSpacers="/Users/kj/Documents/Research/Dr. Gage
Research/Thesis/FastA files for AGREP test/Matching/TA26_spacers.fasta"
for * in filename
do
agrep -3 * to file im comparing to
#potentially may need to use nested loop but not sure 发布于 2019-04-30 19:25:51
好吧,我想我现在明白了。这应该能让你开始。
#!/bin/bash
document="documentToSearchIn.txt"
grep -v spacer fileWithSearchStrings.txt | while read srchstr ; do
echo "Searching for $srchstr in $document"
echo agrep -3 "$srchstr" "$document"
done如果这看起来是正确的,在echo之前删除agrep并再次运行。
如果如您在注释中所述,您希望将脚本存储在其他地方,比如在$HOME/bin中,您可以这样做:
mkdir $HOME/bin将上面的脚本保存为$HOME/bin/search。现在,通过以下方式使其可执行(只需一次):
chmod +x $HOME/bin/search现在将$HOME/bin添加到您的路径中。所以,找到开始的行:
export PATH=...在登录配置文件中,并将其更改为包含新目录:
export PATH=$PATH:$HOME/bin然后启动一个新的终端,您应该能够运行:
search如果希望能够指定字符串文件和要搜索的文档的名称,可以将代码更改为:
#!/bin/bash
# Pick up parameters, if supplied
# 1st param is name of file with strings to search for
# 2nd param is name of document to search in
str=${1:-""}
doc=${2:-""}
# Ensure name of strings file is valid
while : ; do
[ -f "$str" ] && break
read -p "Enter strings filename:" str
done
# Ensure name of document file is valid
while : ; do
[ -f "$doc" ] && break
read -p "Enter document name:" doc
done
echo "Search for strings from: $str, searching in document: $doc"
grep -v spacer "$str" | while read srchstr ; do
echo "Searching for $str in $doc"
echo agrep -3 "$str" "$doc"
done然后你就可以跑:
search path/to/file/with/strings path/to/document/to/search/in或者,如果你像这样跑:
search它会问你两个文件名。
https://stackoverflow.com/questions/55923279
复制相似问题