Easy regex to grep exact match with examples


Written by - Deepak Prasad

How do I grep for an exact match for string or pattern from a file in Linux or Unix. How to search for exact pattern. How to grep exact match with examples?

In this tutorial I will share multiple commands which can be used to grep exact word or string or pattern from a file. 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.

Below is my sample file to demonstrate all the examples and scenarios from this tutorial.

# 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

Now with grep we have an argument (-w) which is used to grep for exact match of whole word from a file.

# 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

As you observe, it did filtered the output by removing non-relevant match although the grep was not 100% successful. From the man page of grep :

-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.

 

Method 1: grep for first and last character

We can grep an exact match by putting a regex match of beginning(^) and ending($) char. Since we are planning to grep for "abcd", our command would be:

# grep -E "^abcd$" /tmp/somefile
abcd
  • But if you observe, this command failed to capture other lines containing "abcd".
  • You have to understand, when we define regex to look for beginning(^) and end($) character, it means first and last character on the entire line.
  • Since there was only one such occurrence where the line starts with "a" and ends with "d", there was single output.
  • This does not work if you have to find anything somewhere in the middle of line.

If your string is in the starting then you can just use (^)

# grep -E "^abcd" /tmp/somefile
abcd some text
abcd

 

Method 2: Match text with white space characters

We can only search for exact match with leading or trailing white space characters so we know that it is exact match

# grep -E "(^| )abcd( |$)" /tmp/somefile
second line abcd some text
abcd some text
abcd

This gives us the perfect output of all the lines with exact "abcd" match. Or use "\s" instead of ( ) white space

# grep -E "(^|\s)abcd(\s|$)" /tmp/somefile
second line abcd some text
abcd some text
abcd

 

Method 3: Match beginning and end of word

With grep extended regex we can match the begin and end of the word

# grep -E "\babcd(\s|$)" /tmp/somefile
second line abcd some text
abcd some text
abcd

Here (\s|$) means, ending with white space or at the end of the line while /b is considered as word boundary and it matches the empty string at the edge of a word. You could have also used:

# grep -E "(\s|^)abcd(\s|$)" /tmp/somefile
second line abcd some text
abcd some text
abcd

OR use /s+ to match one or more white space character while place \b at the end of the text.

# grep -E "(^|\s+)abcd\b" /tmp/somefile
second line abcd some text
abcd some text
abcd

 

Method 4: Match with numbers in the string

Now we will try to print all the lines having 1234abcd. Here our string contains integers, now assuming we don't know the integer values which can be there, all we know that there are some integers at the starting of "abcd"

# grep -E "[0-9]+abcd( |$)" /tmp/somefile
sixth line 1234abcd some text

Here [0-9]+ will match one ore more integers followed by "abcd" while ( |$) will make sure either the string is in the end of the line of has a white space character in the end

 

Conclusion

In this tutorial I showed you multiple grep examples to match exact pattern or string using regex. The exact command may differ based on your requirement, these were some of the common use cases where you can grep exact match with some basic regex.

Lastly I hope this tutorial to search and print exact match in Linux and Unix was helpful. So, let me know your suggestions and feedback using the comment section.

Views: 11

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

2 thoughts on “Easy regex to grep exact match with examples”

  1. Hi there for example if i had the follow result from blkid

    # blkid | grep "Slack64"
    /dev/sda3: LABEL="Tactinu1-Slack64" UUID="17ba0fc9-aa7c-4154-8afc-1c9f5ce47071" TYPE="ext3" PARTUUID="d6cda63a-03"
    /dev/sdb5: LABEL="Slack64" UUID="22df6b3e-2a3a-467b-995e-4fdd08adcc68" TYPE="ext3" PARTUUID="3e59dc84-05"
    /dev/nbd0p1: LABEL="Slack64-15.0" UUID="9f661872-9c60-4045-b123-e5e980b84e07" TYPE="ext3"

    and i just need the exact “Slack64” name, in this case are on the middle, which what command i can get it, without specifing is the middle line ?

    Reply

Leave a Comment