【发布时间】:2019-03-25 08:34:07
【问题描述】:
我要做的是使用agrep 获取文件中最匹配的单词以及错误数。现在我只能使用这个脚本来获取单词:
array=(bla1 bla2 bla3)
for eachWord in "${array[@]}"; do
result=$(yes "yes" | agrep -B ${eachWord} /home/victoria/file.txt)
printf "$result\n"
done
其中 bla{1,2,3} 是一些单词。
我的输出如下:
agrep: 4 words match within 2 errors; search for them? (y/n)counting
first
and
should
agrep: 1 word matches within 1 error; search for it? (y/n)should
agrep: 2 words match within 4 errors; search for them? (y/n)must
must
agrep: 1 word matches within 2 errors; search for it? (y/n)should
有什么方法可以让我得到错误的数量(2,1,4,2 在上面的输出示例中)?
【问题讨论】:
-
你想要什么?
-
我的 Levenstein 距离和最佳匹配词
-
据我了解,您希望输出为:2 1 4 2(即错误数)。你可以试试这个:
result=$(yes "yes" | agrep -B ${eachWord} /home/victoria/file.txt|sed -E -n 's/.*\s+within\s+([0-9]+)\s+errors\;.*/\1/p')。我很确定,这可以使用sed或awk来完成。 -
在此之后我得到的是以下内容。有没有办法只提取第二个数字? : agrep: 4 个单词在 2 个错误内匹配;寻找他们? (y/n) agrep: 1 个单词匹配 1 个错误;寻找它? (y/n) agrep: 2 个单词在 4 个错误内匹配;寻找他们? (y/n) agrep: 1 个单词在 2 个错误内匹配;寻找它? (是/否)
标签: linux bash shell grep agrep