【问题标题】:shell - display number of errors for best matches in agrepshell - 在 agrep 中显示最佳匹配的错误数
【发布时间】: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')。我很确定,这可以使用sedawk 来完成。
  • 在此之后我得到的是以下内容。有没有办法只提取第二个数字? : agrep: 4 个单词在 2 个错误内匹配;寻找他们? (y/n) agrep: 1 个单词匹配 1 个错误;寻找它? (y/n) agrep: 2 个单词在 4 个错误内匹配;寻找他们? (y/n) agrep: 1 个单词在 2 个错误内匹配;寻找它? (是/否)

标签: linux bash shell grep agrep


【解决方案1】:

主要问题是,agrep 将错误报告给标准错误(文件描述符 2)而不是标准输出(文件描述符 1)。为了丢弃stdout并返回stderr,你必须将stdout重定向到/dev/null,并将stderr重定向到stdout:

2>&1 1>/dev/null

小问题是,agrep 不会输出正确的行尾,如果你通过yes 提供它。你必须在 stderr 中写一个换行符:

echo >&2

最后,正如User123 告诉你的,你需要一个sed 命令来提取错误的数量。

这是一个例子:

for a in r1234t rot ruht rood; do
  yes y | agrep -B "$a" /etc/passwd
  echo >&2
done 2>&1 1>/dev/null |
sed -n 's/.* \([0-9]\+\) error.*/\1/p'

输出是:

4
1
2
1

【讨论】:

  • 我猜OP只想要第二个数字(更准确地说,错误的数量:字符串'errorrs'之前的数字),所以sed命令可以修改一下:@987654331 @
猜你喜欢
  • 2011-08-08
  • 2017-04-07
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 2018-05-31
  • 1970-01-01
  • 2013-11-15
  • 2017-11-21
相关资源
最近更新 更多