gpt4 book ai didi

unix - 使用 UNIX 查找准确的单词

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

我想在 Unix 中搜索精确的单词模式。示例:Log.txt 文件包含以下文本:

aaa
bbb
cccaaa ---> this should not be counted in grep output looking for aaa

我正在使用以下代码:

count=$?
count=$(grep -c aaa $EAT_Setup_BJ3/Log.txt)

这里的输出应该是 ==> 1 而不是 2,使用上面的代码我得到 2 作为输出。缺少一些东西,所以有人可以帮我解决这个问题吗?

最佳答案

使用全字选项:

grep -c  -w aaa $EAT_Setup_BJ3/Log.txt

来自 grep 手册:

-w, --word-regexp

Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character.

正如注释中所指出的,-w 是一个 GNU 扩展。对于非 GNU grep,您可以使用单词边界:

grep -c "\<aaa\>" $EAT_Setup_BJ3/Log.txt

关于unix - 使用 UNIX 查找准确的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25404340/

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