[Posts] grep -v, -e, -E

grep -v, -e, -E

Today, we try to use the - v can exclude part of feeling good 

needs to enhance learning. 

HTTPS: // www.jianshu.com/p/4ec50fdaf388

 

12018.08.27 20:27:16 number of words to read 34 333 613

Outline

How to use or, and, not operators do in the Linux grep command?

In fact, the grep command, there are options or equivalent and not operators, but did not grep and this operator. But then, you can use patterns to simulate and operations. The following will give some examples to illustrate how to use or, and, not in the Linux grep command.

In the following example, we will use this employee.txt file, as follows:

$ cat employee.txt  
100  Thomas  Manager    Sales $5,000 200 Jason Developer Technology $5,500 300 Raj Sysadmin Technology $7,000 400 Nisha Manager Marketing $9,500 500 Randy Manager Sales $6,000 

grep or operator

The following four methods can achieve grep OR operation. Method 3 personal recommendation.

  1. Use \ |

If you do not use the grep command of any of the options, you can use '|' to separate multiple pattern, in order to achieve OR operation.

grep 'pattern1\|pattern2' filename  

Examples are as follows:

$ grep 'Tech\|Sales' employee.txt  
100  Thomas  Manager Sales $5,000 200 Jason Developer Technology $5,500 300 Raj Sysadmin Technology $7,000 500 Randy Manager Sales $6,000 
  1. Use the option -E

grep -E option can be used to expand the options for regular expressions. If the grep command option -E, you should use the | to split multiple pattern, in order to achieve an OR operation.

grep -E 'pattern1|pattern2' filename

Examples are as follows:

$ grep -E 'Tech|Sales' employee.txt  
100 Thomas Manager Sales $5,000 200 Jason Developer Technology $5,500 300 Raj Sysadmin Technology $7,000 500 Randy Manager Sales $6,000 
  1. Use egrep

egrep command is equivalent to 'grep -E'. Thus, using egrep (with no options) command, in order to achieve an OR operation according to Pattern plurality of divided.

egrep 'pattern1|pattern2' filename  

Examples are as follows:

$ egrep 'Tech|Sales' employee.txt  
100  Thomas  Manager Sales $5,000 200 Jason Developer Technology $5,500 300 Raj Sysadmin Technology $7,000 500 Randy Manager Sales $6,000 
  1. Use the -e option

Use grep -e option, only pass a parameter. A single command using a plurality -e option, to obtain a plurality of pattern, in order to achieve an OR operation.

grep -e pattern1 -e pattern2 filename

Examples are as follows:

$ grep -e Tech -e Sales employee.txt 100 Thomas Manager Sales $5,000 200 Jason Developer Technology $5,500 300 Raj Sysadmin Technology $7,000 500 Randy Manager Sales $6,000 

grep and operation

  1. Use -E 'pattern1. * Pattern2'

grep command itself does not provide the AND function. However, using the -E option to realize the AND operation.

grep -E 'pattern1.*pattern2' filename  
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename 

The first example is as follows :( wherein two sequence pattern is specified)

$ grep -E 'Dev.*Tech' employee.txt  
200 Jason Developer Technology $5,500 

The second example :( two sequential pattern is not fixed, may be scrambled)

$ grep -E 'Manager.*Sales|Sales.*Manager' employee.txt  
  1. The use of multiple grep command

Grep command can use multiple, separated by the pipe symbol, in order to achieve AND semantics.

grep -E 'pattern1' filename | grep -E 'pattern2'  

Examples are as follows:

$ grep Manager employee.txt | grep Sales  
100  Thomas  Manager    Sales       $5,000  
500  Randy   Manager    Sales       $6,000  

grep not operating

  1. Use grep -v option

Use grep -v NOT operation can be achieved. -v option is used to implement the anti-election match (invert match). As can be matched by all lines except the specified pattern.

grep -v 'pattern1' filename

Examples are as follows:

$ grep -v Sales employee.txt  
200  Jason Developer Technology $5,500 300 Raj Sysadmin Technology $7,000 400 Nisha Manager Marketing $9,500 
  1. It can NOT operation and other operations to unite, in order to achieve a more powerful combination.

As, in this example obtained: 'Manager or Developer, but not Sales' results:

$ egrep 'Manager|Developer' employee.txt | grep -v Sales  
200  Jason   Developer  Technology  $5,500  
400  Nisha   Manager    Marketing   $9,500 

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11583682.html