We can extend our search to subdirectories and any files they contain using the -r option, which tells grep to perform its search recursively. Notice that each line starts with the specific file where that match occurs. When the command is executed, the shell expands the asterisk to the name of any file it finds (in the current directory) which ends in ". Instead of specifying product-listing.html, we can use an asterisk (" *") and the. If we have multiple files to search, we can search them all using a wildcard in our FILE name. Searching multiple files using a wildcard Using the -i option, grep finds a match on line 23 as well. What if "our products" appears at the beginning of a sentence, or appears in all uppercase? We can specify the -i option to perform a case-insensitive match: Performing case-insensitive grep searches Our matching line is prefixed with " 18:" which tells us this corresponds to line 18 in our file. If we specify the -n option, grep will prefix each matching line with the line number: It will be even more useful if we know where the matching line appears in our file. Viewing line numbers of successful matches If we use the -color option, our successful matches will be highlighted for us: For more information, see: Regular expression quick reference. Other characters have special meanings, however - some punctuation marks, for example. In the above example, all the characters we used (letters and a space) are interpreted literally in regular expressions, so only the exact phrase will be matched. $ egrep 'Manager|Developer' employee.The PATTERN is interpreted by grep as a regular expression. You can also combine NOT with other operator to get some powerful combinations.įor example, the following will display either Manager or Developer (bot ignore Sales). grep -v 'pattern1' filenameįor example, display all the lines except those that contains the keyword “Sales”. i.e It matches all the lines except the given pattern. Using grep -v you can simulate the NOT conditions. $ grep Manager employee.txt | grep Salesĥ00 Randy Manager Sales $6,000 Grep NOT 7. The following example will grep all the lines that contain both “Manager” and “Sales” in the same line. grep -E 'pattern1' filename | grep -E 'pattern2' You can also use multiple grep command separated by pipe to simulate AND scenario. GREP OPTIONS IN LINUX WITH EXAMPLES HOW TONote: Using regular expressions in grep is very powerful if you know how to use it effectively. $ grep -E 'Manager.*Sales|Sales.*Manager' employee.txt The following example will grep all the lines that contain both “Manager” and “Sales” in it (in any order). The following example will grep all the lines that contain both “Dev” and “Tech” in it (in the same order). Grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename But, you can simulate AND using grep -E option. $ grep -e Tech -e Sales employee.txtĥ00 Randy Manager Sales $6,000 Grep AND 5. Use multiple -e option with grep for the multiple OR patterns. grep -e pattern1 -e pattern2 filenameįor example, grep either Tech or Sales from the employee.txt file. Use multiple -e option in a single command to use multiple patterns for the or condition. Using grep -e option you can pass only one parameter. $ egrep 'Tech|Sales' employee.txtĥ00 Randy Manager Sales $6,000 4. Just use the | to separate multiple OR patterns. egrep 'pattern1|pattern2' filenameįor example, grep either Tech or Sales from the employee.txt file. So, use egrep (without any option) and separate multiple patterns for the or condition. Grep OR Using egrepĮgrep is exactly same as ‘grep -E’. $ grep -E 'Tech|Sales' employee.txtĥ00 Randy Manager Sales $6,000 3. If you use the grep command with -E option, you just need to use | to separate multiple patterns for the or condition.įor example, grep either Tech or Sales from the employee.txt file. $ grep 'Tech\|Sales' employee.txtĥ00 Randy Manager Sales $6,000 2. Without the back slash in front of the pipe, the following will not work. grep 'pattern1\|pattern2' filenameįor example, grep either Tech or Sales from the employee.txt file. If you use the grep command without any option, you need to use \| to separate multiple patterns for the or condition. I prefer method number 3 mentioned below for grep OR operator. Use any one of the following 4 methods for grep OR. You already knew that grep is extremely powerful based on these grep command examples. The following employee.txt file is used in the following examples. The examples mentioned below will help you to understand how to use OR, AND and NOT in Linux grep command. But, you can simulate AND using patterns. Question: Can you explain how to use OR, AND and NOT operators in Unix grep command with some examples?Īnswer: In grep, we have options equivalent to OR and NOT operators.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |