grep/egrep end of pattern

Hi

I use arp to get the mac-addresses of my hosts.

 # arp -a | grep 192.168.0.
e1000g0   192.168.0.1            255.255.255.255 o        00:00:00:00:00:01
e1000g0   192.168.0.11          255.255.255.255 o        00:00:00:00:00:02
e1000g0   192.168.0.2            255.255.255.255          00:00:00:00:00:03
e1000g0   192.168.0.22          255.255.255.255 o        00:00:00:00:00:04
e1000g0   192.168.0.3            255.255.255.255          00:00:00:00:00:05
e1000g0   192.168.0.33            255.255.255.255         00:00:00:00:00:06

How can I grep/egrep for just one host? I'd like to have the following:

 # arp -a | grep "192.168.0.1"
e1000g0   192.168.0.1            255.255.255.255 o        00:00:00:00:00:01

and not

  # arp -a | grep "192.168.0.1"
 e1000g0   192.168.0.1            255.255.255.255 o        00:00:00:00:00:01
 e1000g0   192.168.0.11          255.255.255.255 o        00:00:00:00:00:02
 

Try the -w option of grep.

Regards

Thanks. Exactly what I need.
Sorry for my stupid question :wink:

you can also try out as
arp -a | grep "192.168.0.1 "

Why should you use a workaround if there's an option for?

Regards

My words :wink:

But I have an other question:

How can I grep/egrep for exactly two (192.168.1 AND 192.168.2 for example) hosts?

arp -a | egrep '192.168.0.(1|2)'

This egrep delivers four hosts.

arp -a | awk '{print $2}' | grep "192.168.0.[1-2]$"

check this out

Not really :wink:

I'm interested in the MAC Address.

File arp.txt:

e1000g0   192.168.0.1            255.255.255.255 o        00:00:00:00:00:01
e1000g0   192.168.0.11          255.255.255.255 o        00:00:00:00:00:02
e1000g0   192.168.0.2            255.255.255.255          00:00:00:00:00:03
e1000g0   192.168.0.22          255.255.255.255 o        00:00:00:00:00:04
e1000g0   192.168.0.3            255.255.255.255          00:00:00:00:00:05
e1000g0   192.168.0.33            255.255.255.255         00:00:00:00:00:06

Needed Output:

e1000g0   192.168.0.1            255.255.255.255 o        00:00:00:00:00:01
e1000g0   192.168.0.2            255.255.255.255          00:00:00:00:00:03

egrep command at the moment:

# egrep '192.168.0.(1|2)' arp.txt
e1000g0   192.168.0.1            255.255.255.255 o        00:00:00:00:00:01
e1000g0   192.168.0.11          255.255.255.255 o        00:00:00:00:00:02
e1000g0   192.168.0.2            255.255.255.255          00:00:00:00:00:03
e1000g0   192.168.0.22          255.255.255.255 o        00:00:00:00:00:04
egrep '192.168.0.(1 |2 )' arp.txt

This does work here. But is this really a good solution?

As I mentioned earlier:

egrep -w '192.168.0.(1|2)' arp.txt

Regards

Unfortunately its egrep this time. :wink:

 # egrep -w '192.168.0.(1|2)' arp.txt
egrep: illegal option -- w
usage: egrep [ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ...

An alternative with awk:

 awk '$2 ~ /192.168.0.[12]$/' arp.txt

Great! Thats what I need!
I do now use the following:

awk '$2 ~ /192.168.0.(1|2|3)$/'