

Here we examined three different ways to grep multiple strings from a file, pattern matching, basic regular expression and extended regular expressions. Some people find certain ways easier to remember, or just more efficient. There are always several ways to accomplish the same task in Linux. You can set an alias for grep to egrep for convenience. In extended regexp we do not need to escape the pipe because it is supported as an OR operator. Or, we can use egrep: egrep 'Pattern1|Pattern2' filename We can use the extended regexp by using the -E option with grep. grep 'motion\|winced' HarBerg.txt Using Extended Regular Expressions to Grep Multiple StringsĮxtended Regular Expressions support the OR operator. Here we will use the same patterns and text file as above, but using the basic regular expressions syntax. grep 'Pattern1\|Pattern2' filenameīasic regexp do not support the pipe ( | ) as an OR operator, so we have to escape it with a backslash. Here is an example of using basic regular expressions to find two strings in a file using grep. The grep command supports both basic and extended regular expressions. grep -e "winced" -e "motion" HarBerg.txt Using Basic Regular Expressions to Grep Multiple Strings Here is an example of searching for the word winced, and the word motion, in the text of the Harrison Bergeron short story that is saved as HarBerg.txt. You can specify several patterns by using the -e switch. Using Pattern Matching to Grep Multiple Strings This is not meant to be a regex primer, but they are an important tool for matching several patterns. Here we will discuss pattern matching, basic regular expressions and extended regular expression. As with everything in Linux, there are several ways to accomplish the same task. GNU grep includes several meta-characters that consist of a backslash followed by a regular character.There are several ways you can match multiple strings or patterns from a file using grep.

The ? quantifier makes the (fear) group optional: grep -E '(fear)?less' file.txt Special Backslash Expressions # The following example matches both “fearless” and “less”. When using basic regular expressions, the parenthesis must be escaped with a backslash ( \). Grouping is a feature of the regular expressions that allows you to group patterns together and reference them as one item. If you use the extended regular expression, then the operator | should not be escaped, as shown below: grep -E 'fatal|error|critical' /var/log/nginx/error.log Grouping # In the example below, we are searching for all occurrences of the words fatal, error, and critical in the Nginx logĮrror file: grep 'fatal\|error\|critical' /var/log/nginx/error.log This operator has the lowest precedence of all regular expression operators. The alternation operator | (pipe) allows you to specify different possible matches that can be literal strings or expression sets. The only difference is that in basic regular expressions the meta-characters ?, +, ' file.txt Alternation # In GNU’s implementation of grep there is no functional difference between the basic and extended regular expression syntaxes. To interpret the pattern as an extended regular expression, use the -E ( or -extended-regexp) option. In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. GNU grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. A pattern consists of operators, constructs literal characters, and meta-characters, which have special meaning. Grep Regular Expression #Ī regular expression or regex is a pattern that matches a set of strings.
Grep multiple how to#
In this article, we’re going to explore the basics of how to use regular expressions in the GNU version of grep, which is available by default in most Linux operating systems.
