![]() Word-constituent characters are letters, digits, and the underscore. Similarly, it must be either at the end of the line or followed by a non-word constituent character. ![]() The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. From the man page of grep: -w, -word-regexp Select only those lines containing matches that form whole words. # grep -w abcd /tmp/somefile second line abcd some text fourth line 12.abcd.32 some text fifth line s(abcd)e some text abcd some text abcdĪs you observe, it did filtered the output by removing non-relevant match although the grep was not 100% successful. ![]() Now with grep we have an argument ( -w) which is used to grep for exact match of whole word from a file. # cat /tmp/somefile first line ABCD some text second line abcd some text third line12abcde some text fourth line 12.abcd.32 some text fifth line s(abcd)e some text sixth line 1234abcd some text seventh line 1abcd234 some text eighth line 234abcd1 some text abcd some text abcd grep exact match with -w I have tried to cover some of the common scenarios, let me know if you face any issues or have additional requirement and I can try to help you via the comments section.īelow is my sample file to demonstrate all the examples and scenarios from this tutorial. In this tutorial I will share multiple commands which can be used to grep exact word or string or pattern from a file. How do I grep for an exact match for string or pattern from a file in Linux or Unix.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |