Example of the or,and,not operation of the grep command

Source: Internet
Author: User
Tags egrep

How do I use the or,and,not operator in the grep command of Linux?

In fact, in the grep command, there are equivalent options for the OR and not operators, but there is no grep and this operator. However, you can use patterns to simulate and manipulate. Here are some examples of how or,and,not is used in the grep command of Linux.

In the following example, this Employee.txt file is used, as follows:

$ cat employee.txt100  Thomas  Manager    Sales       $5,000200  Jason   Developer  Technology  $5,500300  Raj     Sysadmin   technology  $7,000400  Nisha   Manager    Marketing   $9,500500  Randy   Manager    Sales       $6,000

(i) Grep OR operator

The following four methods can be used to achieve the operation of grep or. Personal recommendation Method 3.

1. Using \|

If you do not use any of the options of the grep command, you can use ' \| ' to split multiple pattern to implement or.

 
grep ' pattern1\|pattern2 ' filename

Examples are as follows:
$ grep ' tech\| Sales ' employee.txt100  Thomas  Manager    sales       $5,000200  Jason   Developer  Technology  $5,500300  Raj     Sysadmin   technology  $7,000500  Randy   Manager    Sales       $6,000

2. Use option-E

The GREP-E option can be used to extend the option to a regular expression. If you use the option-E for the grep command, you should use | To divide multiple pattern to implement or operations.

<span style= ' font-family: "Microsoft Yahei"; font-size:16px; ' >grep-e ' pattern1|pattern2 ' filename</span>
Examples are as follows:
$ Grep-e ' tech| Sales ' employee.txt100  Thomas  Manager    sales       $5,000200  Jason   Developer  Technology  $5,500300  Raj     Sysadmin   technology  $7,000500  Randy   Manager    Sales       $6,000

3. using Egrep

The Egrep command is equivalent to ' grep-e '. Therefore, use the egrep (without any option) command to implement or operations based on multiple pattern splits.

Egrep ' pattern1|pattern2 ' filename

Examples are as follows:

$ Egrep ' tech| Sales ' employee.txt100  Thomas  Manager    sales       $5,000200  Jason   Developer  Technology  $5,500300  Raj     Sysadmin   technology  $7,000500  Randy   Manager    Sales       $6,000

4. use option-e

With the GREP-E option, only one parameter can be passed. Use multiple-e options in a single command to get multiple pattern to implement or operations.

GREP-E pattern1-e pattern2 filename

examples are as follows:

$ grep-e tech-e Sales employee.txt100  Thomas  Manager    sales       $5,000200  Jason   Developer  Technology  $5,500300  Raj     Sysadmin   technology  $7,000500  Randy   Manager    Sales       $6,000

(ii)Grep and Operation

1. use-e ' pattern1.*pattern2 '

The grep command itself does not provide and functionality. However, the and operation can be implemented using the-e option.

Grep-e ' pattern1.*pattern2 ' filenamegrep-e ' pattern1.*pattern2|pattern2.*pattern1 ' filename

The first example is as follows: (where the order of two pattern is specified)

$ Grep-e ' Dev.*tech ' employee.txt200  Jason   Developer  technology  $5,500

The second example: (the order of two pattern is not fixed, can be disorderly)

$ Grep-e ' manager.*sales| Sales.*manager ' Employee.txt

2. use multiple grep commands

You can use multiple grep commands, separated by pipe characters, to implement and semantics.

Grep-e ' pattern1 ' filename | Grep-e ' pattern2 '

examples are as follows:

$ grep Manager Employee.txt | grep Sales100  Thomas  manager    Sales       $5,000500  Randy   manager    Sales       $6,000

(iii) GREP not Operation

1. Use the option grep-v

Use GREP-V to implement not operations. The-v option is used to implement the inverse selection match (invert match). For example, a match is obtained for all lines except for the specified pattern.

Grep-v ' pattern1 ' filename

Examples are as follows:

$ grep-v Sales employee.txt200  Jason   Developer  technology  $5,500300  Raj     Sysadmin   Technology  $7,000400  Nisha   Manager    Marketing   $9,500
of course, the not operation can be combined with other operations to achieve a more powerful combination of functions.

For example, this will get: ' Manager or developer, but not ' sales ' result:

$ Egrep ' manager| Developer ' Employee.txt | Grep-v Sales

Jason Developer Technology $5,500

Nisha Manager Marketing $9,500

Example of the or,and,not operation of the grep command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.