Grep-v,-E,-E

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:

[Plain] view plaincopy
    1. $ cat Employee.txt
    2. Thomas Manager Sales $5,000
    3. Jason Developer Technology $5,500
    4. Raj Sysadmin Technology $7,000
    5. Nisha Manager Marketing $9,500
    6. 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.

[Plain] view plaincopy
    1. grep ' pattern1\|pattern2 ' filename


Examples are as follows:

[Plain] view plaincopy
    1. $ grep ' tech\| Sales ' Employee.txt
    2. Thomas Manager Sales $5,000
    3. Jason Developer Technology $5,500
    4. Raj Sysadmin Technology $7,000
    5. 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.

[Plain] view plaincopy
    1. Grep-e ' pattern1|pattern2 ' filename


Examples are as follows:

[Plain] view plaincopy
    1. $ Grep-e ' tech| Sales ' Employee.txt
    2. Thomas Manager Sales $5,000
    3. Jason Developer Technology $5,500
    4. Raj Sysadmin Technology $7,000
    5. 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.

[Plain] view plaincopy
    1. Egrep ' pattern1|pattern2 ' filename

Examples are as follows:

[Plain] view plaincopy
    1. $ Egrep ' tech| Sales ' Employee.txt
    2. Thomas Manager Sales $5,000
    3. Jason Developer Technology $5,500
    4. Raj Sysadmin Technology $7,000
    5. 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.

[Plain] view plaincopy
    1. GREP-E pattern1-e pattern2 filename

Examples are as follows:

[Plain] view plaincopy
    1. $ grep-e tech-e Sales employee.txt
    2. Thomas Manager Sales $5,000
    3. Jason Developer Technology $5,500
    4. Raj Sysadmin Technology $7,000
    5. 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.

[Plain] view plaincopy
    1. Grep-e ' pattern1.*pattern2 ' filename
    2. Grep-e ' pattern1.*pattern2|pattern2.*pattern1 ' filename

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

[Plain] view plaincopy
    1. $ Grep-e ' Dev.*tech ' employee.txt
    2. Jason Developer Technology $5,500

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

[Plain] view plaincopy
    1. $ 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.

[Plain] view plaincopy
    1. Grep-e ' pattern1 ' filename | Grep-e ' pattern2 '

Examples are as follows:

[Plain] view plaincopy
    1. $ grep Manager Employee.txt | grep Sales
    2. Thomas Manager Sales $5,000
    3. 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.

[Plain] view plaincopy
    1. Grep-v ' pattern1 ' filename

Examples are as follows:

[Plain] view plaincopy
    1. $ grep-v Sales Employee.txt
    2. Jason Developer Technology $5,500
    3. Raj Sysadmin Technology $7,000
    4. 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:

[Plain] view plaincopy
      1. $ Egrep ' manager| Developer ' Employee.txt | Grep-v Sales
      2. Jason Developer Technology $5,500
      3. Nisha Manager Marketing $9,500

Grep-v,-E,-E

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.