简体   繁体   中英

Grep exact word UNIX

I have a file and I want to grep one specific word from that file. I am using MobaXterm. For example, I want to grep the word test-common and inside my file I have

 1. Version: 1.0.0    test-common Author Created Path   
 2. Version: 1.0.0    test-common-config   Author   Created Path    
 3. Version: 1.0.0    unit-test-common   Author   Created Path   
 4. Version: 1.0.0    unit-test-common-config   Author   Created Path  

I tried:

  • grep test-common file
  • grep "\\\\< test-common \\\\>" file (don't return anything)
  • grep -w test-common file
  • grep -w "test-common " file
  • grep -r "\\btest-common\\b" file (don't return anything)
  • grep -sw test-common file (don't work)
  • grep -o test-common file (don't work)
  • grep \\\\< bil-common \\\\> file

Also ^bil-common and bil-common$ is not working either.

For the commands I had a result, the result was:

test-common
test-common-config

Which is not what I want. Any help is appreciated.

You can do:

grep -E '(^|\s+)test-common(?=\s|$)' file

This matches "test-common" that are followed only by a space or at the end of the line, and are preceded by one or more space, or at the beginning of the line.

1)I think that the easy way is to just do:
Add to string space before and after (in case you know that it is in the middle of a string)

       grep " string " file

2) if all of the answers doesn't work i would check that grep is not aliased by you:

   witch grep

or add

\\ sign before the grep making sure you use unix grep

      \grep " string " file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM