1

I have input that looks like the following.


name 12.3.14.132 root-servers_net-ipv4
name 19.17.191.123 root-servers_net-ipv4-1
name 192.223.210.110 root-servers_net-ipv4-2
name 172.7.1.31 root-servers_net-ipv4-3
name 1.12.6.14 root-servers_net-ipv4-4

I'm trying to return only the following output.


name 12.3.14.132 root-servers_net-ipv4

grep '^name.* ' test_asa_policy |grep -w "root-servers_net-ipv4"

It appears -w doesn't recognize hyphenated words.

I tried the following but it returned no results

grep -P '(^|\s)\Kroot-servers_net-ipv4(?=\s|$)' test_asa_policy
  • 1
    -w matches all 5 lines because (1) ends in end-of-line, and (2..5) have a non-word character after the search text. – Jeff Schaller Aug 30 '17 at 14:54
2

Just use regexp "end of the string" anchor $:

grep '^name.*root-servers_net-ipv4$' test_asa_policy

The output:

name 12.3.14.132 root-servers_net-ipv4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.