Grep linux несколько значений

Grep OR – Grep AND – Grep NOT – Match Multiple Patterns

The grep , egrep , sed and awk are the most common Linux command line tools for parsing files.

From the following article you’ll learn how to match multiple patterns with the OR , AND , NOT operators, using grep , egrep , sed and awk commands from the Linux command line.

I’ll show the examples of how to find the lines, that match any of multiple patterns, how to print the lines of a file, that match each of provided patterns and how to find and print the lines, that do not match a pattern (negative matching).

Cool Tip: Find and validate email addresses with grep command! The best regular expression for email addresses! Read more →

GREP OR: Match Any Of Multiple Patterns

Find all the lines of a file, that match any of provided patterns.

Using grep and egrep commands:

$ grep "PATTERN1\|PATTERN2" FILE $ grep -E "PATTERN1|PATTERN2" FILE $ grep -e PATTERN1 -e PATTERN2 FILE $ egrep "PATTERN1|PATTERN2" FILE
$ awk '/PATTERN1|PATTERN2/' FILE
$ sed -e '/PATTERN1/b' -e '/PATTERN2/b' -e d FILE

GREP AND: Match Multiple Patterns

It is also often required to grep a file for multiple patterns – when it is needed to find all the lines in a file, that contain not one, but several patterns.

Note, that you can both find the lines in a file that match multiple patterns in the exact order or in the any order.

Use one of the following commands to find and print all the lines of a file, that match multiple patterns.

Using grep command (exact order):

$ grep -E 'PATTERN1.*PATTERN2' FILE

Using grep command (any order):

$ grep -E 'PATTERN1.*PATTERN2|PATTERN2.*PATTERN1' FILE $ grep 'PATTERN1' FILE | grep 'PATTERN2'

Cool Tip: The server is out of memory? Check what processes are using all the RAM and SWAP! Bash one liner for the true Linux admins! Read more →

Читайте также:  Операционная система linux 2012

Using awk command (exact order):

$ awk '/PATTERN1.*PATTERN2/' FILE

Using awk command (any order):

$ awk '/PATTERN1/ && /PATTERN2/' FILE

Using sed command (exact order):

$ sed '/PATTERN1.*PATTERN2/!d' FILE

Using sed command (any order):

$ sed '/PATTERN1/!d; /PATTERN2/!d' FILE

GREP NOT: Negative Matching

Cool Tip: Find and validate IP addresses with grep command! The best regular expression for IP addresses! Read more →

Find and print all the lines, that do not match a pattern.

Источник

How to Grep for Multiple Strings, Patterns or Words

Grep is a powerful utility available by default on UNIX-based systems. The name stands for Global Regular Expression Print.

By using the grep command, you can customize how the tool searches for a pattern or multiple patterns in this case. You can grep multiple strings in different files and directories. The tool prints all lines that contain the words you specify as a search pattern.

In this guide, we will show you how to use grep to search multiple words or string patterns. Follow the examples in this tutorial to learn how to utilize grep most effectively.

Tutorial How to Search Multiple Strings Using grep Command

  • Linux or UNIX-like system
  • Access to a terminal or command line
  • A user with permissions to access the necessary files and directories

How to Grep Multiple Patterns – Syntax

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path.

The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.

grep 'pattern1\|pattern2' fileName_or_filePath

The latest way to use grep is with the -E option. This option treats the pattern you used as an extended regular expression.

grep -E 'pattern1|pattern2' fileName_or_filePath

The deprecated version of extended grep is egrep.

egrep 'pattern1|pattern2' fileName_or_filePath

Another option is to add multiple separate patterns to the grep command.

To do so, use the -e flag and keep adding the desired number of search patterns:

grep -e pattern1 -e pattern2 fileName_or_filePath

What is the Difference Between grep, grep -E, and egrep?

The egrep command is an outdated version of extended grep. It does the same function as grep -E .

The difference between grep and extended grep is that extended grep includes meta characters that were added later.

These characters are the parenthesis (), curly brackets <>, and question mark. The pipe character | is also treated as a meta character in extended grep.

Читайте также:  Узнать время linux команда

Examples of Using Grep for Multiple Strings, Patterns and Words

To make sure you understand how to use grep to search multiple strings, we suggest creating a file with some text on which we are going to try out a couple of different use cases.

In our case, we named the file sample.txt and added a few paragraphs of text. We stored the file in the directory of the test user, that is, in /home/test/sample.txt

How to Grep Multiple Patterns in a File

In the examples below, we will use grep instead of extended grep. Do not forget to use the backslash before the pipe character.

Since grep does not support the pipe symbol as the alternation operator, you need to use the escape character (backslash \) to tell the grep command to treat the pipe differently.

For example, to search for the words extra and value in the sample.txt file use this command:

grep 'extra\|value' sample.txt

The output highlights the string you wanted to grep.

Terminal output when searching two words with grep.

If the same file is in another directory, you need to navigate to that directory or use the full path of the file:

grep 'extra\|value' /home/test/Desktop/sample.txt

To search for more than two words, keep adding them in the same manner.

For example, to search for three words, add the desired string of characters followed by a backslash and pipe:

grep 'extra\|value\|service' sample.txt

Terminal output when searching three words with grep.

Let’s see how the above grep command looks when using grep -E , egrep , and grep -e :

grep -E ‘extra|value|service’ sample.txt
egrep ‘extra|value|service’ sample.txt
grep -e extra -e value -e service sample.txt

We will use grep in further examples, but you can use whichever syntax you prefer.

Search for Multiple Exact Matches in a File

If you want to find exact matches for multiple patterns, pass the -w flag to the grep command.

grep -w 'provide\|count' sample.txt

For example, the output below shows the difference between searching without -w and with it:

Terminal output when searching multiple exact matches with grep.

As you can see, the results are different. The first command shows all lines with the strings you used.

The second command shows how to grep exact matches for multiple strings. The output prints only the lines that contain the exact words.

Note: Grep offers many functionalities. Learn how to use grep for additional use cases.

Ignore Case when Using Grep for Multiple Strings

To avoid missing something when you search for multiple patterns, use the -i flag to ignore letter case.

Читайте также:  List all packages installed on linux

For example, we will ignore case with this command:

grep -i 'phoenix\|linux' sample.txt

Output for grep command to ignore case while searching for multiple patterns.

The output shows how the two commands differ. If you include the -i flag and ignore letter case, the result for multiple matches includes all matches.

This way, you get additional results. If you also add the -w flag to this command, you can narrow down the results even further:

Terminal output when ignoring case while searching two exact matches with grep.

Show the Count of Multiple Matches in a File

Let’s say you are monitoring a log file, and you want to see if the number of warnings or messages increases. You don’t want to see detailed results when a large number of matches return.

For example, to show the count of multiple matches in the bootstrap.log file, enter:

grep -c 'warning\|error' /var/log/bootstrap.log

The output for the count of multiple matches for the grep command.

The output prints the number of matches. This way, you can quickly determine if the number of warnings and errors increased.

Grep for Multiple Patterns in a Specific File Type

You can use grep to search multiple strings in a certain type of file only. If you want to monitor log files in one directory or if you want to search through all text files, use an asterisk and the file extension instead of a file name.

For example, to search for warnings and errors through all .log files in the /var/log/ directory, enter:

grep 'warning\|error' /var/log/*.log

To better demonstrate how this option works, we will only show the count of matches.

Terminal output when searching for multiple patterns in a specific file type with grep.

The output shows all the files that grep searched through for the strings you used.

Note: If you get a “Permission denied” message, as we did in the example above, you need sudo privileges. To include all files, use sudo with the grep command. Enter the sudo password, and grep will search through all files.

Search Recursively for Multiple Patterns in a File

The grep command searches only in the current directory when you use the asterisk wildcard.

To include all subdirectories when searching for multiple patterns, add the -R operator to grep:

grep -R 'warning\|error' /var/log/*.log

The output will return results from all files the grep command found in the /var/log/ directory and its subdirectories.

In this tutorial, you learned how to use grep to search multiple words or string patterns in a file. The guide also showed you how to use extended grep.

The examples in this article help you practice how to refine your grep search. Also, check out our grep regex guide to learn more.

Источник

Оцените статью
Adblock
detector