Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Grep two words together in terminal

Posted on November 9, 2022November 9, 2022 by admin

Table of Contents

  • Overview
  • Approach 1
  • Approach 2
  • Approach 3

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

©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme
Menu
  • Home
  • Posts
  • System Design Questions