Grep OR Using egrepĮgrep is exactly same as ‘grep -E’. $ grep -E 'Tech|Sales' employee.txtĥ00 Randy Manager Sales $6,000 3. Just use the | to separate multiple OR patterns. 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. Instead, if you want to print only the filenames that contain the pattern use the -l (lowercase L) option.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. txt files in the current directory and its subdirectories with line numbers at the beginning.īy default, grep prints the lines which contain the match. This command searches the string "Linux" recursively (-R option not needed as globstar ** takes care of it) in all. The -n option tells grep to print line numbers where the search pattern is found, followed by a colon and the actual text of the line. Lets look into some useful options to control the grep results.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |