grep文件shell中的确切字符串

Tor*_*ren 2 shell grep

我有一个包含两行的主机文件:

1.1.1.1主持人

1.2.3.4 host-MY

我想grep该行只包含主机字符串(而不是包含host-MY的其他行)

我使用:grep -x host/etc/hosts但-x搜索整行匹配提前谢谢

Ben*_*ery 6

编辑:破折号被视为单词分隔符.试试这个:

grep -E '(^|[[:space:]])host($|[[:space:]])' /etc/hosts
Run Code Online (Sandbox Code Playgroud)

老帖子:

您可以使用:

grep -w host /etc/hosts
Run Code Online (Sandbox Code Playgroud)

这在Solaris上运行正常/usr/xpg4/bin/grep.

便携版本将是:

grep -E '\<host\>' /etc/hosts
Run Code Online (Sandbox Code Playgroud)