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.
Hi there for example if i had the follow result from blkid
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 ?
You can use something like this
# blkid | sed ‘s/^.*\(Slack64\).*$/\1/’