How To Grep Multiple Strings, Patterns or Regex in A Text File In Linux?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/grep-multiple-strings-patterns-regex-text-file-linux/
PINTEREST
PINTEREST
INSTAGRAM

grep  provides a lot of features to match strings, patterns or regex in a given text. One of the most used feature is to match two or more, multiple string, patterns or regex. In this tutorial we will look different examples about these features. If you need more general tutorial about regex please look following article.

How To Use Regular Expression – Regex In Bash Linux?

Grep -E or Egrep

Before starting examples we look different commands which provides same functionality. We can use grep  command with -E  option or egrep command which is the alias of the grep -E . They are both the same. In this tutorial we will follow grep -E. -E means extended grep which will enable extended regular expression features to use.

Match Multiple Strings

We will start with the simplest example. We will match given multiple strings inside a given text. In this example we will use a file named data.txt as a text. The matching strings will be ismail  and ali .

Match Multiple Strings
Match Multiple Strings

Match Multiple Pattern or Regex

Now we want to use multiple regular expression or pattern in our match term. We can specify standard regular expression with the same way. We will change some letters with . in this example.

Match Multiple Pattern or Regex
Match Multiple Pattern or Regex

Match IP Address or Domain Name

Now we will look useful example which provides IP address or domain names. This may be a regular operation while searching in log files. We will use [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}  as IP Address regex and  \.[a-z]{1,20}\.[a-z]+ as domain name regex.

LEARN MORE  Powershell Comparison Operators Like Equal, Greater, Lesser, Contains, Regex

Match IP Address or Domain Name
Match IP Address or Domain Name

Match In Multiple Files

We have also the ability to search and match in multiple files. We can use bash glob feature for this. We will use * for the all names and .txt to match text files in this example.

Match In Multiple Files
Match In Multiple Files

As we can see matched files also printed with the matched text.

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/grep-multiple-strings-patterns-regex-text-file-linux/
PINTEREST
PINTEREST
INSTAGRAM