Grep Multiple Strings
The short answer
To match multiple strings or patterns at once with [.inline-code]grep[.inline-code], you can either repeat the [.inline-code]-e[.inline-code] flag multiple times:
Or you can use the [.inline-code]\|[.inline-code] separator:
For example, both of these commands will search for the strings [.inline-code]ERROR[.inline-code] and [.inline-code]WARNING[.inline-code] in the [.inline-code]logs.txt[.inline-code] file and output the matching lines:
Note that, by default, [.inline-code]grep[.inline-code] is case sensitive. You can learn more about that by reading our other article on how to make grep case insensitive.
[#use-extended-regular-expressions] Using extended regular expressions [#use-extended-regular-expressions]
To enable [.inline-code]grep[.inline-code] to use the extended regular expression syntax (like [.inline-code]egrep[.inline-code] would) , you can use the [.inline-code]-E[.inline-code] flag instead of the [.inline-code]-e[.inline-code] flag as follows:
Note that when using this flag, you don't need to escape the [.inline-code]|[.inline-code] pattern separator.
For example, considering the following [.inline-code]logs[.inline-code] file:
This command will output all the lines containing the string [.inline-code]ERROR[.inline-code] followed by a space character and 3-digit numbers:
And this command will output all the lines starting with [.inline-code]2024-06[.inline-code], followed by two digit numbers, followed by the [.inline-code]ERROR[.inline-code] or [.inline-code]WARN[.inline-code] string:
[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering [.inline-code]grep multiple patterns[.inline-code] in the AI Command Suggestions will prompt a [.inline-code]grep[.inline-code] command that can then be quickly inserted in your shell by doing CMD+ENTER .
[#load-patterns-from-a-file] Searching for patterns stored in a text file [#load-patterns-from-a-file]
To search for a list of patterns stored in the file, you can use the [.inline-code]-f[.inline-code] flag as follows:
For example, this command will load a list of patterns contained in the [.inline-code]patterns.txt[.inline-code] file and match them against the lines of the [.inline-code]logs.txt[.inline-code] file:
Extracting text before, after, and between patterns
[#extract-text-before-a-pattern] Extracting text before a pattern [#extract-text-before-a-pattern]
To extract the text before a specified pattern, you can use the [.inline-code]grep[.inline-code] command with the [.inline-code]-oP[.inline-code] flags and the [.inline-code]?=[.inline-code]expression as follows:
For example, considering the following [.inline-code]logs[.inline-code] file:
This command will extract the text before the [.inline-code] ERROR[.inline-code] or [.inline-code] WARN[.inline-code] strings:
[#extract-text-after-a-pattern] Extracting text after a pattern [#extract-text-after-a-pattern]
On the other hand, to extract the text after a specified pattern, you can use the [.inline-code]?<=[.inline-code] expression instead:
For example, considering the following [.inline-code]logs[.inline-code] file:
This command will extract the text after the [.inline-code]Action=[.inline-code] string:
[#extract-text-between-patterns] Extracting text between two patterns [#extract-text-between-patterns]
To extract the text between two patterns, you can combine the two previous expressions as follows:
For example, considering the following [.inline-code]logs[.inline-code] file:
This command will extract the text located between the [.inline-code]UserID=[.inline-code] and [.inline-code] Action[.inline-code] strings: