编写一个脚本,该脚本使用agrep逐个循环文档中的行与另一个文档中的行并获得结果

问题描述 投票:1回答:1

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

enter image description here

如果由于某些奇怪的原因无法看到图像,我也在这里包含了链接。如果您需要我解释更多,请告诉我。这是我的第一篇文章,所以我不确定这将如何被察觉或者我是否使用了正确的术语:)

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 
bash loops fasta agrep
1个回答
0
投票

好吧,我想现在就明白了。这应该让你开始。

#!/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添加到您的PATH中。所以,找到行开头:

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

它会问你2个文件名。

© www.soinside.com 2019 - 2024. All rights reserved.