Linux

How to Grep Way for Various Strings and Patterns

How to Grep Way for Various Strings and Patterns

grep is a powerful command line tool that allows you to search for one or more input files for lines that match regular expressions and write each line that matches standard output.

In this article, we will show you how to use GNU grep to find strings or patterns.

Grep Many Patterns

GNU grep supports three regular expression syntax, Basic, Extended, and Perl-compatible. When no regular expression type is specified, grep interprets the search pattern as a basic regular expression.

To search for multiple patterns, use the OR operator (alternation).

Substitution operator | (pipe) allows you to specify different possible matches that can be either literal strings or expression sets. This operator has the lowest priority of all regular expression operators.

The syntax for searching multiple patterns using grep basic regular expressions is as follows:

$ grep 'pattern1\|pattern2' file...

Always include regular expressions in single quotes to avoid interpretation and extension of meta-characters by the shell.

When using basic regular expressions, meta-characters are interpreted as literal characters. To preserve the special meaning of meta-characters, they must escape with a backslash (\). This is why we escaped from the OR (|) operator with a slash.

To interpret the pattern as an extended regular expression, activate the grep -E (or –-extended-regexp) option. When using an expanded regular expression, do not release | | operator:

$ grep -E 'pattern1|pattern2' file...

Grep Multiple Strings

String literals are the most basic pattern.

In the following example, we are looking for all occurrences of fatal, error, and critical words in the Nginx error log file:

$ grep 'fatal\|error\|critical' /var/log/nginx/error.log

If the string you are looking for includes spaces, enclose it in double quotes.

Following is the same example using extended regular expressions, which eliminates the need to escape from operator |

$ grep -E 'fatal|error|critical' /var/log/nginx/error.log

By default, grep is case sensitive. This means that uppercase and lowercase characters are treated differently.

To ignore cases when searching, activate grep with the -i (or –ignore-case) option:

$ grep -i 'fatal\|error\|critical' /var/log/nginx/error.log

When searching for a string, grep will display all lines where the string is embedded in the larger string. So, if you are looking for “errors”, grep will also print lines where “errors” are embedded in larger words, such as “without errors” or “antiterrorist”.

To return only lines where the specified string is the whole word (enclosed by non-word characters), use the -w option (or –word-regexp):

$ grep -w 'fatal\|error\|critical' /var/log/nginx/error.log

Word characters include alphanumeric characters (a-z, A-Z, and 0-9) and underscores (_). All other characters are considered non-word characters.

For more details about the grep option, visit our Grep command article.

Conclusion

We have shown you how to look for several patterns, strings, and words.

If you have questions or feedback, don’t hesitate to leave a comment.

Related posts

How to Make a Bootable Linux USB Drive Using Linux

Howto

What You Need to Know About Locked in Ubuntu 20.04

Linux

Fedora 31: How to Install Fuel CMS with Nginx

Howto

How to use the chmod command on Linux

Howto

How to Install Notepadqq on Ubuntu 20.04, a decent Notepad ++ alternative

Linux

How to Install Android Studio on Ubuntu 20.04

Linux

How to Install Flatpaks on Ubuntu 20.04

Linux

How to Play PS2 Games on Ubuntu Using PCSX2

Linux

How to Install the NoSQL Apache CouchDB Database on CentOS 8

Linux