10/12/2023 0 Comments Grep for multiple stringsIf 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 #Grep for multiple strings fullIf 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. #Grep for multiple strings how toLet 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. Note that even if you typed data as input to the first grep, you would only get any lines that contain 'word2' shown on the output.Īt one time, the answer used: grep -E 'word1.*word2|word2.*word1' 'word1.*word2' triggered the comments below.Grep is a powerful utility to search strings, patterns and words in files, and text outputs of commands. Now you have two processes running, the first grep and the shell, and they are both trying to read from the keyboard, and it is not determinate which one gets any given line of input (or any given EOF indication). With Heirloom Shell, the second grep completes and exits, and the shell prompts again. With Bash, the parent shell waits until all the processes in the pipeline have exited, so it sits around waiting for the grep -c to finish, which it won't do until you indicate EOF. Note that the second grep doesn't read its standard input at all, so the pipe is superfluous. Or, in my case, it will fail since there's neither a file nor a directory called logs where I'm running the pipeline. The second grep does a recursive search for 'word2' in the files underneath directory logs (or, if it is a file, in the file logs). by typing Control-D), it will sit there, waiting for you to type something. The first grep is going to count the number of occurrences of 'word1' on its standard input, and print that number on its standard output. The initial script is: grep -c "word1" | grep -r "word2" logs If you run two commands, you should aim to select the less frequently occurring word in the first grep to minimize the amount of data processed by the second. Against that, the regex is more complex you might need to benchmark it to find out what's best - but only if performance really matters. However, if the file is huge and 'word1' occurs frequently, then you may be passing significant data down the pipe where a single command avoids that overhead. If the file is big but only a few lines contain 'word1', there isn't going to be much data passed on the pipe and using two command is fine. If the file is small, performance isn't likely to be an issue and running two commands is fine. How much this matters depends on how big the data file is and how many lines match 'word1'. The 'one command' variants have the advantage that there is only one process running, and so the lines containing 'word1' do not have to be passed via a pipe to the second process. If you know that 'word1' will precede 'word2' on the line, you don't even need the alternatives and regular grep would do: grep 'word1.*word2' logs You could use extended grep ( grep -E or egrep): grep -E 'word1.*word2|word2.*word1' logs However, it isn't necessary to use two commands like that. The first grep finds lines with 'word1' from the file 'logs' and then feeds those into the second grep which looks for lines containing 'word2'. One simple rewrite of the command in the question is: grep "word1" logs | grep "word2"
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |