How to Read Multiple String From a File and Compare Them C++

Introduction

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

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

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

Tutorial How to Search Multiple Strings Using grep Command

Prerequisites

  • Linux or UNIX-like organization
  • Access to a concluding 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 exist enclosed using single quotes and separated by the pipage symbol. Use the backslash earlier pipage | for regular expressions.

          grep 'pattern1\|pattern2' fileName_or_filePath        

The latest style to use grep is with the -Due east option. This choice treats the pattern yous used equally an extended regular expression.

          grep -Eastward 'pattern1|pattern2' fileName_or_filePath        

The deprecated version of extended grep is egrep.

          egrep 'pattern1|pattern2' fileName_or_filePath        

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

To exercise so, utilise the -e flag and continue calculation the desired number of search patterns:

          grep -e pattern1 -eastward pattern2 fileName_or_filePath        

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

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

The divergence between grep and extended grep is that extended grep includes meta characters that were added after.

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

Examples of Using Grep for Multiple Strings, Patterns and Words

To make certain you sympathise how to utilise grep to search multiple strings, we suggest creating a file with some text on which we are going to endeavor 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 beneath, nosotros volition utilize grep instead of extended grep. Do not forget to use the backslash before the pipe grapheme.

Since grep does not support the pipage symbol as the alternation operator, you lot need to apply the escape graphic symbol (backslash \) to tell the grep control to treat the pipe differently.

For instance, to search for the words extra and value in the sample.txt file utilize 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' /habitation/test/Desktop/sample.txt        

To search for more than two words, continue calculation them in the aforementioned mode.

For case, 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.

Permit's see how the higher up 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 -east extra -e value -e service sample.txt        

We will employ grep in further examples, but yous can utilize whichever syntax yous prefer.

Search for Multiple Verbal Matches in a File

If yous want to discover exact matches for multiple patterns, pass the -west flag to the grep control.

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

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

Terminal output when searching multiple exact matches with grep.

Every bit you can meet, the results are dissimilar. The first control shows all lines with the strings you used.

The second control 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 employ 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.

For case, 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 y'all include the -i flag and ignore letter instance, the event for multiple matches includes all matches.

This fashion, you get boosted results. If you also add the -west flag to this command, you lot tin 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'south say y'all are monitoring a log file, and yous want to see if the number of warnings or messages increases. You don't want to run into detailed results when a big number of matches render.

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

          grep -c 'warning\|fault' /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 tin quickly determine if the number of warnings and errors increased.

Grep for Multiple Patterns in a Specific File Type

You lot tin can use grep to search multiple strings in a certain type of file only. If you desire to monitor log files in ane directory or if you desire to search through all text files, utilise 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 'alarm\|fault' /var/log/*.log        

To improve 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 yous used.

Notation: If you lot get a "Permission denied" message, equally we did in the instance above, you demand sudo privileges. To include all files, utilise sudo with the grep command. Enter the sudo password, and grep volition search through all files.

Search Recursively for Multiple Patterns in a File

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

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

          grep -R 'alert\|mistake' /var/log/*.log        

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

Determination

In this tutorial, you lot learned how to use grep to search multiple words or cord patterns in a file. The guide as well showed you how to use extended grep.

The examples in this article help you do how to refine your grep search.

Was this article helpful?

Aye No

durganlumospors.blogspot.com

Source: https://phoenixnap.com/kb/grep-multiple-strings

0 Response to "How to Read Multiple String From a File and Compare Them C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel