0

I am unable to grep for exact word match containing hyphen as in

/home/imper-home,3,0,0,0,jim.imper,NONE,NONE,NONE,http://sanjose                                                                                                                                 
/home/imper,15,10,3,30,jim.imper,NONE,NONE,NONE,http://sanjose-age

I tried

grep -w imper  

but it returns both /home/imper-home and /home/imper.

I want only /home/imper-home to returned by using,

grep -wv /home/imper
pacholik
  • 8,607
  • 9
  • 43
  • 55
anudeep
  • 415
  • 6
  • 19
  • `grep 'imper-' file` ? – 123 Apr 19 '16 at 10:51
  • Have a look at the accepted answer of http://stackoverflow.com/questions/17616012/grep-extract-only-whole-word – blackSmith Apr 19 '16 at 10:53
  • I need it in general for any variable not only for "imper".Excluding hyphen in word match in grep – anudeep Apr 19 '16 at 10:55
  • @blackSmith that did not work. I had checked it before posting this question. – anudeep Apr 19 '16 at 10:56
  • 1
    If you have access to the `p` flag, why don't you simply use `grep -P '(/home/imper)(?=,)' filename --color`. Regarding the answer I mentioned, I just wanted to make you aware of the issue with `-`. – blackSmith Apr 19 '16 at 10:58
  • its a solaris server, unfortunately -P option doesnt work in there for grep – anudeep Apr 19 '16 at 13:22
  • 2
    If @123's answer doesn't work for you, the question is not clear enough. Moreover, it seems like you're trying to solve x, but asking for y which is the wrong approach to start with. – Rany Albeg Wein Apr 19 '16 at 13:40
  • as I said I need to grep any particular string as in "black" and "black-home" or "black-art" , if I use grep -w "black" all those return.I do not want that. – anudeep Apr 19 '16 at 16:58
  • The question is still unclear to me, and Rany's concern about this being an [XY Problem](http://mywiki.wooledge.org/XyProblem) seems valid. Please edit your question, and include your underlying problem, the *reason* you think you want to do this. It may very well be that there's another way to solve your problem that is easier or better. – ghoti Apr 21 '16 at 06:25

1 Answers1

0

This will work in general:

word=imper
grep -w "$word" file | grep -v "$word-"
webb
  • 4,180
  • 1
  • 17
  • 26