Fri, 25 Apr 2025 05:28:30 +0000
The “grep” command is short for “Global Regular Expression Print”. This is a powerful tool in Unix-based systems used to search and filter text based on specific patterns. If you work with too many text-based files like logs, you will find it difficult to search for multiple strings in parallel. “grep” has the ability to search for multiple strings simultaneously, streamlining the process of extracting relevant information from files or command outputs. In this article, let us explain the variants of grep, instructions on how to use grep multiple string search, practical examples, and some best practices. Let’s get started!
“grep” and Its Variants
At Unixmen, we always start with the basics. So, before diving into searching for multiple strings, it’s necessary to understand the basic usage of “grep” and its variants:
- grep: Searches files for lines that match a given pattern using basic regular expressions.
- egrep: Equivalent to “grep -E”, it interprets patterns as extended regular expressions, allowing for more complex searches. Note that “egrep” is deprecated but still widely used.
- fgrep: Equivalent to “grep -F”, it searches for fixed strings rather than interpreting patterns as regular expressions.
You are probably wondering why we have two functions for doing the same job. egrep and grep -E do the same task and similarly, fgrep and grep -F have the same functionality. This is a part of a consistency exercise to make sure all commands have a similar pattern. At Unixmen, we recommend using grep -E and grep -F instead of egrep and fgrep respectively so that your code is future-proof.
Now, let’s get back to the topic. For example, to search for the word “error” in a file named “logfile.txt”, your code will look like:
grep "error" logfile.txt
How to Search for Multiple Strings with grep
There are multiple approaches to use grep to search for multiple strings. Let us learn each approach with some examples.
Using Multiple “-e” Options
The “-e`” option lets you specify multiple patterns. Each pattern is provided as an argument to “-e”:
grep -e "string1" -e "string2" filename
This command searches for lines containing either “string1” or “string2” in the specified file.
Using Extended Regular Expressions with “-E”
By enabling extended regular expressions with the “-E” option, you can use the pipe symbol “|” to separate multiple patterns within a single quoted string:
grep -E "string1|string2" filename
Alternatively, you can use the “egrep” command, which is equivalent to grep -E, but we do not recommend it considering egrep is deprecated.
egrep "pattern1|pattern2" filename
Both commands will match lines containing either “pattern1” or “pattern2”.
Using Basic Regular Expressions (RegEx) with Escaped Pipe
In basic regular expressions, the pipe symbol “|” is not recognized as a special character unless escaped. Therefore, you can use:
grep "pattern1\|pattern2" filename
This approach searches for lines containing either “pattern1” or “pattern2” in the specified file.
Practical Examples
Now that we know the basics and the multiple methods to use grep to search multiple strings, let us look at some real-world applications.
How to Search for Multiple Words in a File
If you have a file named “unixmen.txt” containing the following lines:
alpha bravo charlie delta fox golf kilo lima mike
To search for lines containing either “alpha” or “kilo”, you can use:
grep -E "apple|kiwi" sample.txt
The output will be:
apple banana cherry kiwi lemon mango
Searching for Multiple Patterns in Command Output
You can also use grep to filter the output of other commands. For example, to search for processes containing either “bash” or “ssh” in their names, you can use:
ps aux | grep -E "bash|ssh"
This command will display all running processes that include “bash” or “ssh” in their command line.
Case-Insensitive Searches
To perform case-insensitive searches, add the “-i” option:
grep -i -e "string1" -e "string2" filename
This command matches lines containing “string1” or “string2” regardless of case.
How to Count Number of Matches
To count the number of lines that match any of the specified patterns, use the “-c” option:
grep -c -e "string1" -e "string2" filename
This command outputs the number of matching lines.
Displaying Only Matching Parts of Lines
To display only the matching parts of lines, use the “-o” option:
grep -o -e "string1" -e "string2" filename
This command prints only the matched strings, one per line.
Searching Recursively in Directories
To search for patterns in all files within a directory and its subdirectories, use the “-r” (short for recursive) option:
grep -r -e "pattern1" -e "pattern2" /path/to/directory
This command searches for the specified patterns in all files under the given directory.
How to Use awk for Multiple String Searches
While “grep” is powerful, there are scenarios where “awk” might be more suitable, especially when searching for multiple patterns with complex conditions. For example, to search for lines containing both “string1” and “string2”, you can use:
awk '/string1/ && /string2/' filename
This command displays lines that contain both “string1” and “string2”.
Wrapping Up with Some Best Practices
Now that we have covered everything there is to learn about using grep to search multiple strings, it may feel a little overwhelming. Hereβs why it is worth the effort.
“grep” can be easily integrated into scripts to automate repetitive tasks, like finding specific keywords across multiple files or generating reports. It’s widely available on Unix-like systems and can often be found on Windows through tools like Git Bash or WSL. Knowing how to use “grep” makes your skills portable across systems. Mastering grep enhances your problem-solving capabilities, whether you’re debugging code, parsing logs, or extracting specific information from files. By leveraging regular expressions, grep enables complex pattern matching, which expands its functionality beyond simple string searches.
In short, learning grep is like gaining a superpower for text processing. Once you learn it, youβll wonder how you ever managed without it!
Related Articles
- How to Refine your Search Results Using Grep Exclude
- VI Save and Exit: Essential Commands in Unix’s Text Editor
- Why It Is Better to Program on Linux
The post grep: Multiple String Search Feature appeared first on Unixmen.
Recommended Comments