Find the answer to your Linux question:
Results 1 to 4 of 4
This is on a RHEL 6 box with bash 4.1.2 I'm trying to to use grep to only find those lines containing matches that form whole words. The -w option ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    15

    Using grep with hyphens


    This is on a RHEL 6 box with bash 4.1.2

    I'm trying to to use grep to only find those lines containing matches that form whole words.

    The -w option works fantastic unless of course that word has a hyphen.

    The problem is I will get a hit on "test-group" which is a good thing, but I will also get a hist on "test" which is bad because the group test doesn't exist. It appears that once grep hits a hyphen it treats the preceding text as a whole word.

    Any ideas would be greatly appreciated.

    Next time no groups with hyphens..

    Thanks

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,380
    Quote Originally Posted by woodson2 View Post
    This is on a RHEL 6 box with bash 4.1.2

    I'm trying to to use grep to only find those lines containing matches that form whole words.

    The -w option works fantastic unless of course that word has a hyphen.

    The problem is I will get a hit on "test-group" which is a good thing, but I will also get a hist on "test" which is bad because the group test doesn't exist. It appears that once grep hits a hyphen it treats the preceding text as a whole word.

    Any ideas would be greatly appreciated.

    Next time no groups with hyphens..

    Thanks
    Please post the entire query expression.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    185
    Without know the full query I can only guess

    Code:
    grep test | grep group

  4. #4
    Just Joined!
    Join Date
    Sep 2007
    Posts
    19
    Quote Originally Posted by woodson2 View Post
    This is on a RHEL 6 box with bash 4.1.2

    The -w option works fantastic unless of course that word has a hyphen.
    If I understood your problem correctly, then I suggest you use -E and the regexp yourself ("word" defn. remains same as that of grep with the added "-"). I'm not sure of grep, in awk this might work "[^A-Za-z0-9_\-][A-Za-z0-9_\-]+[^A-Za-z0-9_\-]"... I'm not a Linux m/c now, so can't test it, but I guess/hope you get the idea.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •