top of page
  • Writer's pictureJatin Madaan

Unix grep command to match exact string

Sometimes there is a requirement of matching exact word using grep command for this grep function has one optional parameter and other wildcards to make sure correct word/string is picked.


eg : Let's say we have a file with names Jack and Jacky if we use :


$ cat names.txt |grep Jack

Jack

Jacky


Output would be both to avoid this and only output Jack we can use below 3 methods:


1: -w option


$ cat names.txt | grep -w Jack

Jack


2 : \b characters


$ cat names.txt |grep "\bJack\b"

Jack


3: \< characters


$ cat names.txt |grep "\<Jack\>"

Jack





170 views0 comments

Comments


bottom of page