Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux Regular expressions grep and egrep     - Linux disk management practices (Linux)

- Use Python automatically cleared Android Engineering excess resources (Programming)

- Oracle rebuild index script (Database)

- Linux garbled file delete method (Linux)

- Varnish achieve page jump (Server)

- How to Install Android Studio on Ubuntu 15.04 / CentOS7 (Linux)

- Broadcom transplanted to OpenWrt summary (Programming)

- Between the two to achieve the main MySQL database synchronization from (Database)

- To assign multiple IP addresses NIC on the CentOS 7 (Linux)

- Teach you how to choose to install CentOS 6.5 installation package (Linux)

- AngularJS notes --- Scope and controller (Programming)

- Nginx Keepalived Nginx monitoring scripts (Server)

- You can not ignore the seven Git tips (Linux)

- Ubuntu 14.10 How to install office suite Calligra Suite 2.8.7 (Linux)

- The difference between free command displays the buffers and cache (Linux)

- CentOS 6.5 x86_64 system customized automated deployment (Linux)

- CentOS yum configuration under local sources (Linux)

- The simple multi-threaded Python (Programming)

- Use LKM change the default linux security level (Linux)

- Computer security perimeter recommendations (Linux)

 
         
  Linux Regular expressions grep and egrep
     
  Add Date : 2018-11-21      
         
         
         
  Regular expression: it refers to a single string that describes or matches a set of syntactic rules in line with the string. In many text editors or other tools, the regular expression is often used to retrieve or replace text in line with those of a model.
In fact, regular expressions, just an idea, a representation. As long as the tools we use to support representation of this idea then this tool string regular expression can handle. Common tools grep, sed, awk, these three are the only line of the text for the operation.

grep filter
Syntax: grep [-cinvABC] 'word' filename

-n Display line numbers
-c count the number of rows to meet the requirements of statistics
-v negated, does not contain the selected character
-i case-insensitive
-r directory will traverse all files such as: grep -r 'root' ./
-A Back with numbers, A2 indicates the row and meet the requirements of the following two lines of print
-B Followed by numbers, B2 represents the second line print line and meet the requirements of the above
-C Followed by digital, C2 represents print meet the requirements of each row and the second row up and down
^ Beginning of a line, beginning
$ End of a line, the end of the
^ $ Represented by an empty line

You can make an alias alias grep = "grep --color" written into .bashrc; color key characters enter grep the subsequent display, easy to distinguish.

Line with a keyword filter output and line numbers, colors Keywords
[Root @ localhost ~] # grep -n --color 'root' passwd
1: root: x: 0: 0: root: / root: / bin / bash
11: operator: x: 11: 0: operator: / root: / sbin / nologin
[Root @ localhost ~] # grep -o --color 'root' passwd | wc -l
4

Add -o statistics contain the number of keywords;

Filtration without a keyword line and output line number;
[Root @ linuxhost.com ~] # grep -nv 'nologin' / etc / passwd
1: root: x: 0: 0: root: / root: / bin / bash
6: sync: x: 5: 0: sync: / sbin: / bin / sync
7: shutdown: x: 6: 0: shutdown: / sbin: / sbin / shutdown
8: halt: x: 7: 0: halt: / sbin: / sbin / halt
20: user1: x: 600: 501 :: / home / user1: / bin / bash
23: mysql: x: 27: 27: MySQL Server: / var / lib / mysql: / bin / bash

Filtered to nologin end, the system prohibits the landing of all users;
[Root @ localhost ~] # grep 'nologin $' / etc / passwd
bin: x: 1: 1: bin: / bin: / sbin / nologin
daemon: x: 2: 2: daemon: / sbin: / sbin / nologin
adm: x: 3: 4: adm: / var / adm: / sbin / nologin
lp: x: 4: 7: lp: / var / spool / lpd: / sbin / nologin
mail: x: 8: 12: mail: / var / spool / mail: / sbin / nologin
uucp: x: 10: 14: uucp: / var / spool / uucp: / sbin / nologin

Example, the print keyword halt the row A2 B2 C2
[Root @ linuxhost.com ~] # grep -A2 'halt' passwd
halt: x: 7: 0: halt: / sbin: / sbin / halt
mail: x: 8: 12: mail: / var / spool / mail: / sbin / nologin
uucp: x: 10: 14: uucp: / var / spool / uucp: / sbin / nologin
[Root @ linuxhost.com ~] # grep -B2 'halt' passwd
sync: x: 5: 0: sync: / sbin: / bin / sync
shutdown: x: 6: 0: shutdown: / sbin: / sbin / shutdown
halt: x: 7: 0: halt: / sbin: / sbin / halt
[Root @ linuxhost.com ~] # grep -C2 'halt' passwd
sync: x: 5: 0: sync: / sbin: / bin / sync
shutdown: x: 6: 0: shutdown: / sbin: / sbin / shutdown
halt: x: 7: 0: halt: / sbin: / sbin / halt
mail: x: 8: 12: mail: / var / spool / mail: / sbin / nologin
uucp: x: 10: 14: uucp: / var / spool / uucp: / sbin / nologin

All lines beginning with a # sign removal

[Root @ linuxhost.com ~] # grep -v '^ #' / etc / inittab
id: 3: initdefault:

Remove all blank lines and lines beginning with #

[Root @ linuxhost.com ~] # grep -v '^ #' / etc / crontab | grep -v '^ $'
SHELL = / bin / bash
PATH = / sbin: / bin: / usr / sbin: / usr / bin
MAILTO = root
HOME = /

Example shows, numbers, or start with a letter printed, and instead of letters and numbers at the beginning;
[Root @ linuxhost.com tmp] # cat test.txt
helloworld
abc
abc11111
#differt
12345
67899
123def

[0-9] represents any one number, meaning filter out the entire command line contains any number of;

[Root @ linuxhost.com tmp] # grep '[0-9]' test.txt
abc11111
12345
67899
123def

[^ 0-9] In addition to the representatives of any of the characters 0-9, meaning the entire command is to filter out the line does not contain numbers;

[Root @ linuxhost.com tmp] # grep '[^ 0-9]' test.txt
helloworld
abc
abc11111
#differt
123def

^ [^ 0-9] is not representative of the number at the beginning;

[Root @ linuxhost.com tmp] # grep '^ [^ 0-9]' test.txt
helloworld
abc
abc11111
#differt

[A-z] represents any letters of the alphabet;

[Root @ linuxhost.com tmp] # grep '[a-z]' test.txt
helloworld
abc
abc11111
#differt
123def

[^ A-z], except the representative of the English alphabet;

[Root @ linuxhost.com tmp] # grep '[^ a-z]' test.txt
abc11111
#differt
12345
67899
123def

^ [^ A-z] is not the beginning of the text represents the English alphabet;

[Root @ linuxhost.com tmp] # grep '^ [^ a-z]' test.txt
#differt
12345
67899
123def

[] If it is digital, just use [0-9] in such a way, of course, sometimes you can also use this form [15] that is, containing only 1 or 5, note that it will not be considered to be 15. If you want to filter out the numbers and uppercase and lowercase letters they have written [0-9a-zA-Z]. Also [] Another form is [^ characters] represents character except the characters [] inside of.

Any one character with repeated characters filter
[Root @ linuxhost.com ~] # grep 'h..t' / etc / passwd
halt: x: 7: 0: halt: / sbin: / sbin / halt

'' Stands for any one character, as the above example there are two rows that match any of the characters between h and t filter out.

'*' Represents zero or more of any character
'Ooo *' representatives oo, ooo, oooo or more o
[Root @ linuxhost.com ~] # grep 'ooo *' / etc / passwd
root: x: 0: 0: root: / root: / bin / bash
lp: x: 4: 7: lp: / var / spool / lpd: / sbin / nologin
mail: x: 8: 12: mail: / var / spool / mail: / sbin / nologin
uucp: x: 10: 14: uucp: / var / spool / uucp: / sbin / nologin
operator: x: 11: 0: operator: / root: / sbin / nologin
postfix: x: 89: 89 :: / var / spool / postfix: / sbin / nologin

'*' Means zero or more of any character, equal to all, blank lines are also included.
[Root @ linuxhost.com ~] # grep / etc / passwd '*.' | Wc -l
twenty four
[Root @ linuxhost.com ~] # wc -l / etc / passwd
24 / etc / passwd

Specifies the number of characters that appear in the filter
{} Inside a number that represents the number of times to repeat the previous character. That represents two O OO contain rows. {} Needs to add about off ideographic characters \
grep -E that is representative of an enhanced version of the grep egrep, do not need to take off egrep intended use;
123456789 [root @ linuxhost.com ~] # grep 'o \ {2 \}' / etc / passwd
root: x: 0: 0: root: / root: / bin / bash
lp: x: 4: 7: lp: / var / spool / lpd: / sbin / nologin
mail: x: 8: 12: mail: / var / spool / mail: / sbin / nologin
uucp: x: 10: 14: uucp: / var / spool / uucp: / sbin / nologin
operator: x: 11: 0: operator: / root: / sbin / nologin
postfix: x: 89: 89 :: / var / spool / postfix: / sbin / nologin
[Root @ localhost ~] # grep -E 'o {2}' passwd
[Root @ localhost ~] # egrep 'o {2}' passwd

[Root @ linuxhost.com ~] # cat test.txt
root: hot
abcde
spoool
spool
spol
spl

Example, the letter o filter row appears 1-3 times

[Root @ linuxhost.com ~] # grep 'o \ {1,3 \}' test.txt
root: hot
spoool
spool
spol

{} May also represent a range of the format {n1, n2} n1
egrep Extended version of grep, we can use grep egrep complete work can not be completed, of course can be completed grep egrep can complete.
grep -E = egrep

1, followed by screening of a character or a character in front of more use +
[Root @ linuxhost.com ~] # cat test.txt
rot: x: 0: 0: rot: / rot: / bin / bash
root: x: 0: 0: root: / root: / bin / bash
daemon: x: 2: 2: daemon: / sbin: / sbin / nologin
rooooot: x: 0: 0 / roooooot: / bin / bash
11111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[Root @ linuxhost.com ~] # egrep 'o +' test.txt
rot: x: 0: 0: rot: / rot: / bin / bash
root: x: 0: 0: root: / root: / bin / bash
daemon: x: 2: 2: daemon: / sbin: / sbin / nologin
rooooot: x: 0: 0 / roooooot: / bin / bash
[Root @ linuxhost.com ~] # egrep 'oo +' test.txt
root: x: 0: 0: root: / root: / bin / bash
rooooot: x: 0: 0 / roooooot: / bin / bash
[Root @ linuxhost.com ~] # egrep 'ooo +' test.txt
rooooot: x: 0: 0 / roooooot: / bin / bash

2, the characters behind the character zero or a filter in front of the use?

[Root @ linuxhost.com ~] # egrep 'o?' Test.txt
rot: x: 0: 0: rot: / rot: / bin / bash
root: x: 0: 0: root: / root: / bin / bash
daemon: x: 2: 2: daemon: / sbin: / sbin / nologin
rooooot: x: 0: 0 / roooooot: / bin / bash
11111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[Root @ linuxhost.com ~] # egrep 'oo?' Test.txt
rot: x: 0: 0: rot: / rot: / bin / bash
root: x: 0: 0: root: / root: / bin / bash
daemon: x: 2: 2: daemon: / sbin: / sbin / nologin
rooooot: x: 0: 0 / roooooot: / bin / bash
[Root @ linuxhost.com ~] # egrep 'ooo?' Test.txt
root: x: 0: 0: root: / root: / bin / bash
rooooot: x: 0: 0 / roooooot: / bin / bash
[Root @ linuxhost.com ~] # egrep 'oooo?' Test.txt
rooooot: x: 0: 0 / roooooot: / bin / bash

3, the filter string 1 or character string 2 which contains an arbitrary string print out

[Root @ linuxhost.com ~] # egrep 'aaa | 111 | ooo' test.txt
rooooot: x: 0: 0 / roooooot: / bin / bash
11111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

4, egrep (the) application

[Root @ linuxhost.com ~] # egrep 'r (oo) | (mo) n' test.txt
root: x: 0: 0: root: / root: / bin / bash
daemon: x: 2: 2: daemon: / sbin: / sbin / nologin
rooooot: x: 0: 0 / roooooot: / bin / bash

By () represents a whole, such as (oo) + represents an 'oo' or more 'oo'

[Root @ linuxhost.com ~] # egrep '(oo) +' test.txt
root: x: 0: 0: root: / root: / bin / bash
rooooot: x: 0: 0 / roooooot: / bin / bash

5, egrep in [] Application

Characters in square brackets as one; [^ o] for the addition of the letter o;
Example: at the beginning of the end of the r t ;;
[Root @ localhost ~] # egrep 'r [o] t' test.txt
rot: x: 0: 0: rot: / rot: / bin / bash

O r after the opening there of

[Root @ localhost ~] # egrep 'r [o]' test.txt
rot: x: 0: 0: rot: / rot: / bin / bash
root: x: 0: 0: root: / root: / bin / bash
rooooot: x: 0: 0 / roooooot: / bin / bash

R o the beginning not later;

[Root @ localhost ~] # egrep 'r [^ o]' test.txt
rrt
rtx

t is the front end of the character is not o;

[Root @ localhost ~] # egrep '[^ o] t' test.txt
rrt
rtx

* +? Symbol summary
Indicates any single character (including spaces special characters # $?)
* * Means zero or more of the preceding character
* Represents any number of arbitrary characters (including blank lines)
+ Means one or more of the preceding character +
? Represents 0 or 1? Preceding character
Wherein, +? Grep does not support, egrep supported.

"Ro. * T" to indicate the beginning of ro ​​t until the end of the
[Root @ localhost ~] # grep 'ro. * T' test.txt
rot: x: 0: 0: rot: / rot: / bin / bash
root: x: 0: 0: root: / root: / bin / bash
rooooot: x: 0: 0 / roooooot: / bin / bash
     
         
         
         
  More:      
 
- Hardware Firewall Basics (Linux)
- php performance monitoring module XHProf (Linux)
- CentOS7 minimized installation can not find the 'ifconfig' command - Repair Tips (Linux)
- JITwatch installation under Linux (Linux)
- MySQL and Oracle time zone settings compare (Database)
- Ubuntu 14.04 installed Nvidia CUDA 7.5 and build Python Theano deep learning development environment (Linux)
- Python object-oriented programming (Programming)
- JDK comes with tools jinfo (Linux)
- Java Annotation Comments (Programming)
- Manually generate AWR reports (Database)
- MySQL fuzzy query: LIKE and REGEXP pattern mode (Database)
- Git common skills (Linux)
- Lua non-blocking write log (Programming)
- Oracle set the timer task JOB scheduler to execute stored procedures or PL / SQL code block (Database)
- Linux (RHEL6 CENTOS6 OLE6) VNC-SERVER Installation and Configuration (Server)
- 5 tips to improve your Linux desktop security (Linux)
- Linux server security settings to close unused ports (Linux)
- Replace element and non-replaced elements of learning (Programming)
- How apt-get limited use IPv4 or IPv6 protocol to download (Linux)
- Bash environment is automatically install and initialize oh-my-zsh & autojump zsh (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.