Table of Contents
Overview
Here are sample examples to do it
Let’s create a sample.txt file first
sample.txt
This file
contains word1 and
also word2
Here are some ways to search two words together using grep
Approach 1
- Use \| as a separator between two words that need to be searched
Example
some_user@macOS grep 'word1\|word2' ./sample.txt
contains word1 and
also word2
Approach 2
- Use -e for each word that needs to be searched
Example
some_user@macOS grep -e word1 -e word2 ./sample.txt
contains word1 and
also word2
Approach 3
- Use -E and |
Example
some_user@macOS grep -E 'word1|word2' ./sample.txt
contains word1 and
also word2
Note: Check out our system design tutorial series System Design Questions