How to search for multiple words?

QuestionsQuestions8 SkillsProUser Account ManagementSep, 21 2025
083

To search for multiple words in a file using grep, you can use the -e option for each word you want to search for. Here's an example command:

sudo grep -w -e 'word1' -e 'word2' /etc/passwd

Breakdown:

  • -w: Matches whole words.
  • -e 'word1': Specifies the first word to search for.
  • -e 'word2': Specifies the second word to search for.
  • /etc/passwd: The file being searched.

Alternative Method

You can also use a regular expression with the -E option (extended regex) to search for multiple words:

sudo grep -E -w 'word1|word2' /etc/passwd

In this case, the | acts as a logical OR between the words.

Feel free to ask if you need more examples or further assistance!

0 Comments

no data
Be the first to share your comment!