How to Grep for Multiple Strings, Patterns or Words?

Grep is one of the most powerful and widely used command-line tools in Linux/Unix systems. It stands for "Global Regular Expression Print" and is used for searching text files or output of commands for specific patterns or strings. It can search through an entire directory structure, filter results, and display only relevant data. Grep is versatile for system administration, programming, and data analysis tasks.

Basic Grep Commands

Grep is a command-line tool used in Unix-based operating systems to search for specific patterns or strings in files or command output. The basic syntax is ?

grep [options] pattern [file]

The "pattern" is the string or regular expression you want to search for, and the "file" argument specifies the file to search. If no file name is given, grep reads from standard input. The -i option makes searches case-insensitive.

Single String Search Examples

To search for a single string in a file, use the following syntax ?

grep 'string' filename

For example, to find all occurrences of "apple" in a file named "fruits.txt" ?

grep 'apple' fruits.txt

For pattern matching using regular expressions, you can find words starting with "a" and ending with "le" ?

grep 'a.*le' fruits.txt

This matches words like "apple", "able", and "ankle".

Searching for Multiple Strings

Grep can search for multiple strings or patterns simultaneously using the OR operator (|). This allows finding different patterns in a single command execution.

Using the OR Operator

To search for either "apple" or "banana" in fruits.txt ?

grep 'apple\|banana' fruits.txt

Or using extended regex with -E option ?

grep -E 'apple|banana' fruits.txt

For case-insensitive recursive search in a directory ?

grep -irE 'apple|banana|cherry' fruits_folder/

Multiple Methods Comparison

Method Syntax Use Case
Basic OR grep 'word1\|word2' file Simple multiple string search
Extended Regex grep -E 'word1|word2' file Complex pattern matching
Multiple grep grep 'word1' file | grep 'word2' AND condition (both must exist)

Context-Based Searching

Grep provides context options to display surrounding lines for better understanding of matches ?

  • -A n ? displays n lines after each match

  • -B n ? displays n lines before each match

  • -C n ? displays n lines before and after each match

grep -A 2 -B 1 'error' logfile.txt

Exact Word Matching

The word boundary option (\b) ensures grep matches complete words only, avoiding partial matches. This prevents "cat" from matching "caterpillar" or "scattered".

grep '\bapple\b' fruits.txt

For multiple exact words ?

grep -E '\b(apple|banana|cherry)\b' fruits.txt

Regular Expression Patterns

Regular expressions provide powerful pattern matching capabilities. Common regex characters include ?

  • . ? matches any single character

  • * ? matches zero or more of preceding character

  • ^ ? matches beginning of line

  • $ ? matches end of line

  • [abc] ? matches any character in brackets

Example: Find lines starting with "Error" or "Warning" ?

grep -E '^(Error|Warning)' logfile.txt

Advanced Examples

Search for IP addresses or email patterns ?

grep -E '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' network.log
grep -E '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' contacts.txt

Combine multiple techniques for complex searches ?

grep -irE -C 2 '\b(fatal|critical|emergency)\b' /var/log/

Conclusion

Grep's ability to search for multiple strings, patterns, and words makes it indispensable for text processing and system administration. By combining OR operators, context options, word boundaries, and regular expressions, you can create powerful search queries that efficiently locate exactly the information you need across files and directories.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements