1

我正在尝试编写一个脚本,该脚本使用 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 
4

1 回答 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并再次运行。


如果,正如您在评论中所说,您想将脚本存储在其他地方,例如 in $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 个文件名。

于 2019-04-30T19:25:51.457 回答