gpt4 book ai didi

linux - shell - 显示 agrep 中最佳匹配的错误数

转载 作者:太空宇宙 更新时间:2023-11-04 11:53:18 25 4
gpt4 key购买 nike

我正在尝试做的是使用 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)?

最佳答案

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

2>&1 1>/dev/null

小问题是,如果您输入 yesagrep 不会输出正确的行尾。你必须在 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

关于linux - shell - 显示 agrep 中最佳匹配的错误数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55333850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com