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"
2
  • 1
    arp -a | grep -o "$ip" Commented Jun 23, 2016 at 11:32
  • 1
    That grep will NOT produce that output. edit your question to correct your example. Commented Jun 23, 2016 at 11:35

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.