![]() ![]() ![]() txt files in all subfolders in /home/ubuntu. The above command will also look for your search strings in. $ sudo grep -R 'water|sun' /home/ubuntu/*.txt If you also want to look for multiple strings in different files in subdirectories then use -R option to do a recursive search. txt files in /home/ubuntu directory but not in any of its sub directories. The above command will only look for specified search strings in all. *.txt files) $ sudo grep 'water|sun' /home/ubuntu/*.txt If you want to search for multiple strings in multiple files you can use wildcard characters to mention specific file type (e.g. Search for multiple patterns in multiple files $ sudo grep -c 'water|sun' /home/ubuntu/poem.txtĪlso read : How to Copy File to Multiple Directories If you want to get a count of occurences of your search string in a file, use -c option. $ sudo grep -i 'water|sun' /home/ubuntu/poem.txtĪlso read : How to Create User in MongoDB If you want to ignore case while searching for multiple strings and patterns, use -i option. $ sudo grep -w 'water|sun' /home/ubuntu/poem.txt If you want to search for exact match, use -w option. $ sudo grep -e water -e sun -e moon /home/ubuntu/poem.txtĪlso read : How to Search in Nano Text Editor $ sudo grep -E 'water|sun|moon' /home/ubuntu/poem.txt It will treat each string as a regular expression instead of a literal string, and allows you to include regex characters such as *, ?,, etc. If you want to search for patterns that contain specified strings, use -E option. The above grep command will look for a match for either of the specified strings only. If you want to search 3 strings (water,sun, moon just add it to your search pattern as shown $ sudo grep 'water|sun|moon' /home/ubuntu/poem.txt If you don’t specify the full file path, grep will look for the file in your present working directory. $ sudo grep 'water|sun' /home/ubuntu/poem.txt Let us say you want to search for multiple strings (water, sun) in a file /home/ubuntu/poem.txt. Let us look at examples of different use cases.Īlso read : How to Check if mod_deflate is Enabled In both cases, grep will output all the lines that contain any of the listed patterns. In the first command, we ask grep to look for multiple patterns (pattern1, pattern2, …) in a file while in second case, we pass (pipe) the output of another command to grep so that it can search it for required strings. $ sudo grep 'pattern1|pattern2|.' /path/to/file Here is the basic syntax for grep to look for multiple strings & patterns. How to Grep Multiple Strings, Patterns & Words ![]() In this article, we will look at how to grep multiple strings & patterns in a file or text. Sometimes you may need to look for more than one string in a file or large text. grep command also allows you to customize your search using various options. It allows you to search one or more strings & patterns based on your requirement. Grep is a powerful utility to search strings, patterns and words in files, and text outputs of commands. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |