开发者

grep exact string from file shell

开发者 https://www.devze.com 2023-02-15 15:17 出处:网络
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 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

精彩评论

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