BASH Programming

Grep for Multiple Patterns or Strings

The users of the Linux operating system can use grep to investigate the various patterns or strings in a file. The grep method of multiple strings or patterns can be used if the operating system contains files with multiple strings and the user wants to target or reach the specified two or more strings from the file. The strings and real path of the relevant file are typically included in the grep command. The pipe symbol can be used to divide them. Before the pipe “|”, we can add a backslash to create the regular expressions. To ignore the cases while doing search operations, the users can use the “-i” option when launching the grep program.

Utilization of “Grep” in Multiple Strings and Patterns

To utilize this method in the Linux operating system, we create a text file first on the desktop with the “file.txt” name and then save the statement data in the file to search the string in it. We store the data in this text file on the desktop as provided in the following:


After the creation and addition of data in the file, we move into the terminal of the Linux. Then, we add the command of granting access to the desktop since the “file.txt” directory is a desktop. So, we add the “cd Desktop/” command and the command that we assign to it for the grep of searching out our two strings of “bat” and “ball” which are present in the data of our file. We utilize the following provided command:

Linux@linux:~/Desktop$ grep ‘bat\|ball’ file.txt

 
After adding up the command of grep for multiple strings, we press enter. It displays the string of “bat” and “ball” which are present in the data string with the bold red colored letter as we can see in the following output:

Linux@linux:~$ There are many games but the most loving game in England is played by bat and ball known as cricket.

 
Here, we search for two strings. Now, we see the searching of three strings in the “file.txt” data and search for the “hockey”, “world”, and “badminton” strings in the file. We add up the following command into the terminal:

Linux@linux:~/Desktop$ grep ‘hockey\|world\|badminton’ file.txt

 
Adding up this command in the terminal and pressing enter display the output with the strings with the red colored font on screen by searching these strings in the data which are present in our granted data of “file.txt”. As we can see in the attached textual output, there are three pattern string lines of data. Only three strings get the red colored as we assigned in the command. It majorly targets that for search but goes through reading all the strings which are present in the file.

Linux@linux:~$ People play hockey also and watch it with keen interest.
The Fifa world cup of 2022 is won by Argentina.
I love to play badminton.

 
Now, to have more concepts related to the pattern of strings, we create a new text file of “linux.txt” where we store some statement lines data and utilize the simple “grep –I”. We search the “Linux” and “multiple” string in the file that could perform without any case sensitivity. It might not have any impact on the upper or lower case letter of the string. The command that we implement in the terminal for this string is provided in the following for the “linux.txt” file:

Linux@linux:~/Desktop$ grep –I ‘linux\|multiple’ linux.txt

 
When we add this command to a string and run it, it reads the two particular strings from our file which are present in the string and highlight them with red color, showing that it finds those strings from the pattern. The rest of the unwanted strings are displayed just in a simple manner.

Linux@linux:~$ this is a professional blog related to the linux operating system for the topic under discussion of grep for multiple strings.

 
To have some concepts related to the patterns in grep with searching of strings, we utilize a few more grep commands. We create another new file with the name “name.txt” as a text file and grant it with some names as a string. The command that we utilize only targets the particularly called strings and ignore the rest of the string even if it does not display it on the output screen. The strings that are shown in the output have a sequential arrangement as they were already present in the text file. As we want to get the “Smith” and “Alex” strings, we write them with the “grep –iw” command. The whole command is provided in the following:

Linux@linux:~/Desktop$ grep –iw  ‘Alex\|Smith’ name.txt

 
When we go through the working of this directory, we see that it displays two strings, “Smith” and “Alex”, on the screen.

Linux@linux:~$ Smith
Alex

 
Now, we discuss some concepts of the pattern of some strings. If we want to show a half pattern rather than showing the whole string, we utilize the required string pattern only except for using the full string name. Now, we want to read the “Alex” string in full but we want some pattern of the “Smith” string. We utilize its “Smi” rather than the full string name. For this purpose, we  assign the following command in the Linux terminal:

Linux@linux:~/Desktop$ grep ‘Smi\|Alex’ name.txt

 
After pressing the enter button, it displays the two string names, “Smith” and “Alex”, on the screen. But the “Smith” string shows a half pattern of “Smi” as the red highlighted color. And “th” is not read to be highlighted as it was not granted in the command, so it makes the pattern of a string.

Linux@linux:~$ Smith
Alex

 
To have some relation of pattern along with the error or detection in the string or finding out whether the string or pattern is present in the directory or not, we utilize the grep along with the “-c”. Then, we assign the “Linux” and “abc” with the pattern path as “/home/linux” as our operating system user path location and “*.txt” for the detection of the text file.

Linux@linux:~/Desktop$ grep –c ‘linux\|abc’ /home/Linux/*.txt

 
After running the command, it displays the path with “0” in our Linux files. With the text extension, we simply utilize the specific terms. Arithmetic is used to indicate all content. Zero (0) represents no matching data.

/home/Linux/data.txt: 0
/home/Linux/mh.txt: 0

 

Conclusion

The Linux grep command for multiple patterns or strings is covered in this article. First, we created three files with the names “file.txt”, “linux.txt”, and “name.txt” on our operating system, each of which contains various strings or patterns. Before using the third command on “linux.txt”, the first two commands are used in “the file.txt”. To deal with the numerous strings or patterns included in the “name.txt” file, the fourth and fifth commands are applied.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.