In Linux, the “grep” command is utilized to search or match the text/pattern in files or directories. By default, grep performs a case-sensitive search and returns any lines that contain the specified search pattern. It has various applications to find out specific strings, characters, and words in the entire file.
This article will offer the syntax along with several examples to match exact strings using the “grep” command in Linux.
- How Does the “grep” Command Work to Match the Exact String?
- Exact Match String in a Specified File
- Exact Match String (Case-Insensitive Search)
- Match Multiple Strings (Case Sensitivity)
- Match Exact String in Entire Directory
How Does “grep” Command Work to Exact Match String?
To match the exact string, use the grep command with the “-w” option to force grep to match only whole words. Follow the below syntax along with practical examples:
$ grep -w "matching_string" <filename>
In the above syntax, replace “matching_string” with the string that the user wants to search, and “filename” refers to the name of the file to search.
Example 1: Exact Match String in a Specified File
An example is considered to search the exact string in a file. To do so, the “grep” command is utilized with the “w” option by specifying the string “itslinuxfoss” in the “file.txt“:
$ grep -w "itslinuxfoss" file.txt
It matches the exact string “itslinuxfoss” that is found in the “file.txt” and returns the lines that contain the particular string.
Note: Grep returns nothing in the terminal if the string is not found.
Example 2: Exact Match String (Case-Insensitive Search)
Users can also use the “grep” command with other options to refine their search. For instance, use the “-i” option to perform a case-insensitive search in the “file.txt”:
$ grep -i "itslinuxfoss" file.txt
The output shows that the “itslinuxfoss” string has been exactly matched in the “file.txt” without case sensitivity.
Example 3: Match Multiple Strings (Case Sensitive Search)
To match the multiple strings while considering the case sensitivity, utilize the “grep” command with the specified string pattern and filename. In our case, we specified the “\” with “pipeline” to separate multiple strings for searching:
$ grep 'its\|is' file.txt
The output shows that “its” and “is” strings have been searched in the “file.txt”.
Example 4: Exact Match String in Entire Directory
To match the exact string in the entire directory, users can utilize the “grep” command with the “r” option. In our case, we searched the “itslinuxfoss” string recursively in the home directory:
$ grep -r itslinuxfoss *
It searches the “itslinuxfoss” string recursively in the home directory and its subdirectories.
Note: To explore the “grep” command with different applications, users can follow our article “grep Command in Linux”.
To match the exact string using the “grep” command, use the “grep -w “matching_string” <filename>” command. Users can also specify a string or multiple strings by separating through the pipeline “|”. It finds the specified patterns and extracts the lines where the strings are matched. This article has explained all possible methods to match the string using the “grep” command in Linux.