I have input that looks like the following.

name root-servers_net-ipv4
name root-servers_net-ipv4-1
name root-servers_net-ipv4-2
name root-servers_net-ipv4-3
name root-servers_net-ipv4-4

I'm trying to return only the following output.

name 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

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

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

The output:

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