如何用grep搜索整个单词? [英] How to search for whole words with grep?

查看:1201
本文介绍了如何用grep搜索整个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有文件 foo

read_from_buffer
read_from_buffer_and_file
write_to_buffer
some_other_function

然后使用

cat foo | grep 'read_from_buffer'

会列出2行:

will list 2 lines:

read_from_buffer
read_from_buffer_and_file

但我想只有完全匹配...如何判断 grep :不同的字符必须来自字符: 0-9a-zA-Z _

But I want only exact matches... How to tell grep that different character must come than character: 0-9a-zA-Z_

推荐答案

使用这个:

Use this:

grep -w 'read_from_buffer' foo






man grep


-w, - word-regexp :只选择那些包含全部词的匹配项的行。测试是,匹配的子字符串必须是
或者位于行的开头,或者是以非单词组成字符开头。同样,它必须是
,或者在行尾,或者后面跟着一个非单词组成字符。单词组成字符是
字母,数字和下划线。

-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. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.



or


-x,--line-regexp :仅选择与整行。 (-x由POSIX指定。)

-x, --line-regexp: Select only those matches that exactly match the whole line. (-x is specified by POSIX.)

这篇关于如何用grep搜索整个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆