开发者

grep exact string from file shell

I have a host file with two lines:

1.1.1.1 host

1.2.3.4 host-MY

I'd like to gr开发者_StackOverflow中文版ep the line contains host string only (not the other line that contain host-MY)

I use: grep -x host /etc/hosts but -x search whole line matching Thanks in advance


EDIT: dashes are considered as word separator. Try this one instead:

grep -E '(^|[[:space:]])host($|[[:space:]])' /etc/hosts

Old post:

You can use:

grep -w host /etc/hosts

This works fine on Solaris with /usr/xpg4/bin/grep.

The portable version would be:

grep -E '\<host\>' /etc/hosts


How about this:

grep -E "\s*host\s*$" /etc/hosts


Try

grep -q -E "\bhost\b" /etc/hosts
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜