0

I want to grep exact string by pattern in variable

ip="192.168.100.1"
arp -a | grep "$ip"

This outputs something like this:

# arp -a | grep "$ip"
? (192.168.10.1) at 66:ca:6d:88:57:cd [ether]  on br0
? (192.168.10.15) at 3c:15:a0:05:b5:94 [ether]  on br0

but I want exactly IP no IP of other PCs Also I have only embedded grep (minimalistic) also I have awk,sed.

Im trying this but without success:

arp -a | grep "\b$ip\b"
tester125
  • 21
  • 7

2 Answers2

1

Word boundaries like \b aren't available with standard grep. From the output snippet you posted it looks like this will work for you:

$ ip="192.168.10.1"
$ grep -F "($ip)" file
? (192.168.10.1) at 66:ca:6d:88:57:cd [ether]  on br0

i.e. just use -F for a string instead of regexp comparison and explicitly include the delimiters that appear around the IP address in the input.

FWIW in awk it'd be:

$ awk -v ip="($ip)" 'index($0,ip)' file
? (192.168.10.1) at 66:ca:6d:88:57:cd [ether]  on br0

and you can't do it in a reasonable way in sed since sed ONLY supports regexp comparisons, not strings (see Is it possible to escape regex metacharacters reliably with sed).

Community
  • 1
  • 1
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

If I understand correctly what you are saying you just want to add a -o option to your command, the -o option print only the matched (non-empty) parts of a matching line,with each such part on a separate output line.

arp -a | grep -o "$ip"
mik1904
  • 1,335
  • 9
  • 18