正則表達式的字符串表達方法根據不同的嚴謹程度與功能分為基本正則表達式與擴展正則表達式?;A正則表達式是常用的正則表達式的最基礎的部分。在 Linux 系統(tǒng)中常見的文件處理工具中grep 與 sed 支持基礎正則表達式,掌握基礎正則表達式的使用方法,首先必須了解基本正則表達式所包含的元字符的含義,下面通過 grep 命令以舉例的方式逐個介紹。
1.基礎正則表達式示例
下面的操作我這邊復制一份httpd配置文件作為測試使用。
[root@localhost ~]# cp /etc/httpd/conf/httpd.conf /opt/httpd.txt
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
httpd.txt rh
[root@localhost opt]# cat httpd.txt
#
# This is the main Apache HTTP server configuration file. It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# for a discussion of each configuration directive.
#
# Do NOT simply read the instructions in here without understanding
# what they do. They\\\'re here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
#
# Configuration and logfile names: If the filenames you specify for many
# of the server\\\'s control files begin with / (or drive:/ for Win32), the
# server will use that explicit path. If the filenames do *not* begin
# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
# with ServerRoot set to \\\'/www\\\' will be interpreted by the
# server as \\\'/www/log/access_log\\\', where as \\\'/log/access_log\\\' will be
# interpreted as \\\'/log/access_log\\\'.
#
# ServerRoot: The top of the directory tree under which the server\\\'s
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path. If you point
...//省略部分內容...
1) 查找特定字符
使用grep命令查找特定字符,其中“-n”表示顯示行號、“-i”表示不區(qū)分大小寫
[root@localhost opt]# grep -n the httpd.txt
2:# This is the main Apache HTTP server configuration file. It contains the
3:# configuration directives that give the server its instructions.
9:# Do NOT simply read the instructions in here without understanding
10:# what they do. They\\\'re here only as hints or reminders. If you are unsure
11:# consult the online docs. You have been warned.
13:# Configuration and logfile names: If the filenames you specify for many
14:# of the server\\\'s control files begin with / (or drive:/ for Win32), the
15:# server will use that explicit path. If the filenames do *not* begin
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
17:# with ServerRoot set to \\\'/www\\\' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server\\\'s
25:# Do not add a slash at the end of the directory path. If you point
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
27:# Mutex directive, if file-based mutexes are used. If you wish to share the
35:# ports, instead of the default. See also the <VirtualHost>
47:# To be able to use the functionality of a module which was built as a DSO you
48:# have to place corresponding `LoadModule\\\' lines at this location so the
49:# directives contained in it are actually available _before_ they are used.
62:# User/Group: The name (or #number) of the user/group to run httpd as.
71:# The directives in this section set up the values used by the \\\'main\\\'
74:# any <VirtualHost> containers you may define later in the file.
76:# All of these directives may appear inside <VirtualHost> containers,
77:# in which case these default settings will be overridden for the
...//省略部分內容...
[root@localhost opt]# grep -ni the httpd.txt
2:# This is the main Apache HTTP server configuration file. It contains the
3:# configuration directives that give the server its instructions.
9:# Do NOT simply read the instructions in here without understanding
10:# what they do. They\\\'re here only as hints or reminders. If you are unsure
11:# consult the online docs. You have been warned.
13:# Configuration and logfile names: If the filenames you specify for many
14:# of the server\\\'s control files begin with / (or drive:/ for Win32), the
15:# server will use that explicit path. If the filenames do *not* begin
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
17:# with ServerRoot set to \\\'/www\\\' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server\\\'s
25:# Do not add a slash at the end of the directory path. If you point
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
27:# Mutex directive, if file-based mutexes are used. If you wish to share the
35:# ports, instead of the default. See also the <VirtualHost>
47:# To be able to use the functionality of a module which was built as a DSO you
48:# have to place corresponding `LoadModule\\\' lines at this location so the
49:# directives contained in it are actually available _before_ they are used.
62:# User/Group: The name (or #number) of the user/group to run httpd as.
71:# The directives in this section set up the values used by the \\\'main\\\'
73:# <VirtualHost> definition. These values also provide defaults for
74:# any <VirtualHost> containers you may define later in the file.
76:# All of these directives may appear inside <VirtualHost> containers,
77:# in which case these default settings will be overridden for the
82:# ServerAdmin: Your address, where problems with the server should be
89:# ServerName gives the name and port that the server uses to identify itself.
98:# Deny access to the entirety of your server\\\'s filesystem. You must
99:# explicitly permit access to web content directories in other
...//省略部分內容...
若反向選擇,如查找不包含“the”字符的行,則需要通過 grep 命令的“-vn”選項實現。
[root@localhost opt]# grep -nv the httpd.txt
1:#
4:# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
5:# In particular, see
6:# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
7:# for a discussion of each configuration directive.
8:#
12:#
18:# server as \\\'/www/log/access_log\\\', where as \\\'/log/access_log\\\' will be
19:# interpreted as \\\'/log/access_log\\\'.
20:
21:#
23:# configuration, error, and log files are kept.
24:#
28:# same ServerRoot for multiple httpd daemons, you will need to change at
29:# least PidFile.
30:#
31:ServerRoot /etc/httpd
32:
33:#
34:# Listen: Allows you to bind Apache to specific IP addresses and/or
36:# directive.
37:#
38:# Change this to Listen on specific IP addresses as shown below to
39:# prevent Apache from glomming onto all bound IP addresses.
40:#
41:#Listen 12.34.56.78:80
42:Listen 80
43:
44:#
45:# Dynamic Shared Object (DSO) Support
46:#
50:# Statically compiled modules (those listed by `httpd -l\\\') do not need
51:# to be loaded here
...//省略部分內容...
[root@localhost opt]# grep -n \\\'oo\\\' httpd.txt
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
17:# with ServerRoot set to \\\'/www\\\' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server\\\'s
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
28:# same ServerRoot for multiple httpd daemons, you will need to change at
31:ServerRoot /etc/httpd
54:# LoadModule foo_module modules/mod_foo.so
60:# httpd as root initially and it will switch.
63:# It is usually good practice to create a dedicated user and group for
86:ServerAdmin root@localhost
115:# DocumentRoot: The directory out of which you will serve your
119:DocumentRoot /var/www/html
130:# Further relax access to the default document root:
226: # Redirect permanent /foo http://www.example.com/bar
230: # access content that does not live under the DocumentRoot.
332:#ErrorDocument 500 The server made a boo boo.
358:wood
359:woooood
若查找“oo”前面不是“w”的字符串,只需要通過集合字符的反向選擇“[^]”來實現該目的
[root@localhost opt]# grep -n \\\'[^w]oo\\\' httpd.txt
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
17:# with ServerRoot set to \\\'/www\\\' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server\\\'s
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
28:# same ServerRoot for multiple httpd daemons, you will need to change at
31:ServerRoot /etc/httpd
54:# LoadModule foo_module modules/mod_foo.so
60:# httpd as root initially and it will switch.
63:# It is usually good practice to create a dedicated user and group for
86:ServerAdmin root@localhost
115:# DocumentRoot: The directory out of which you will serve your
119:DocumentRoot /var/www/html
130:# Further relax access to the default document root:
226: # Redirect permanent /foo http://www.example.com/bar
230: # access content that does not live under the DocumentRoot.
332:#ErrorDocument 500 The server made a boo boo.
359:woooood
[root@localhost opt]# grep -n \\\'[^a-z]oo\\\' httpd.txt
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
17:# with ServerRoot set to \\\'/www\\\' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server\\\'s
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
28:# same ServerRoot for multiple httpd daemons, you will need to change at
31:ServerRoot /etc/httpd
115:# DocumentRoot: The directory out of which you will serve your
119:DocumentRoot /var/www/html
230: # access content that does not live under the DocumentRoot.
查找包含數字的行可以通過“grep –n‘[0-9]’ httpd.txt”命令來實現.
[root@localhost opt]# grep -n \\\'[0-9]\\\' httpd.txt
4:# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
6:# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
14:# of the server\\\'s control files begin with / (or drive:/ for Win32), the
41:#Listen 12.34.56.78:80
42:Listen 80
95:#ServerName www.example.com:80
141: # http://httpd.apache.org/docs/2.4/mod/core.html#options
311:# interpretation of all content as UTF-8 by default. To use the
312:# default browser choice (ISO-8859-1), or to allow the META tags
316:AddDefaultCharset UTF-8
329:# 1) plain text 2) local redirects 3) external redirects
332:#ErrorDocument 500 The server made a boo boo.
333:#ErrorDocument 404 /missing.html
334:#ErrorDocument 404 /cgi-bin/missing_handler.pl
335:#ErrorDocument 402 http://www.example.com/subscription_info.html
[root@localhost opt]# grep -n \\\'^[^a-zA-Z]\\\' httpd.txt
1:#
2:# This is the main Apache HTTP server configuration file. It contains the
3:# configuration directives that give the server its instructions.
4:# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
5:# In particular, see
6:# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
7:# for a discussion of each configuration directive.
8:#
9:# Do NOT simply read the instructions in here without understanding
10:# what they do. They\\\'re here only as hints or reminders. If you are unsure
11:# consult the online docs. You have been warned.
...//省略部分內容...
...//省略部分內容...[root@localhost opt]# grep -n \\\'\\\\.$\\\' httpd.txt
3:# configuration directives that give the server its instructions.
4:# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
7:# for a discussion of each configuration directive.
19:# interpreted as \\\'/log/access_log\\\'.
23:# configuration, error, and log files are kept.
29:# least PidFile.
36:# directive.
...//省略部分內容...
在正則表達式中小數點(.)也是一個元字符,代表任意一個字符。例如, 執(zhí)行以下命令就可以查找“wd”的字符串,即共有四個字符,以w 開頭 d 結尾。
[root@localhost opt]# grep -n \\\'w..d\\\' httpd.txt
108:# Note that from this point forward you must specifically allow
148: # It can be All, None, or any combination of the keywords:
358:wood
在上述結果中,“wood”字符串“w..d”匹配規(guī)則。若想要查詢o、oo、ooooo 等資料,則需要使用星號(*)元字符。但需要注意的是,“*”代表的是重復零個或多個前面的單字符。“o*”表示擁有零個(即為空字符)或大于等于一個“o”的字符,因為允許空字符,所以執(zhí)行“grep –n‘o*’ httpd.txt”命令會將文本中所有的內容都輸出打印。如果是“oo*”, 則第一個 o 必須存在,第二個 o 則是零個或多個 o,所以凡是包含 o、oo、ooooo,等的資料都符合標準。同理,若查詢包含至少兩個 o 以上的字符串,則執(zhí)行“grep –n‘ooo*’ httpd.txt”命令即可。
[root@localhost opt]# grep -n \\\'ooo*\\\' httpd.txt
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
17:# with ServerRoot set to \\\'/www\\\' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server\\\'s
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
28:# same ServerRoot for multiple httpd daemons, you will need to change at
31:ServerRoot /etc/httpd
54:# LoadModule foo_module modules/mod_foo.so
60:# httpd as root initially and it will switch.
63:# It is usually good practice to create a dedicated user and group for
86:ServerAdmin root@localhost
115:# DocumentRoot: The directory out of which you will serve your
119:DocumentRoot /var/www/html
130:# Further relax access to the default document root:
226: # Redirect permanent /foo http://www.example.com/bar
230: # access content that does not live under the DocumentRoot.
332:#ErrorDocument 500 The server made a boo boo.
358:wood
359:woooood
[root@localhost opt]# grep -n \\\'w.*d\\\' httpd.txt
...//省略部分內容...
342:# be turned off when serving from networked-mounted
356:wd
357:wod
358:wood
359:woooood
查詢任意數字所在行
[root@localhost opt]# grep \\\'[0-9][0-9]*\\\' httpd.txt
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
# of the server\\\'s control files begin with / (or drive:/ for Win32), the
#Listen 12.34.56.78:80
Listen 80
#ServerName www.example.com:80
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# interpretation of all content as UTF-8 by default. To use the
# default browser choice (ISO-8859-1), or to allow the META tags
AddDefaultCharset UTF-8
# 1) plain text 2) local redirects 3) external redirects
#ErrorDocument 500 The server made a boo boo.
#ErrorDocument 404 /missing.html
#ErrorDocument 404 /cgi-bin/missing_handler.pl
#ErrorDocument 402 http://www.example.com/subscription_info.html
[root@localhost opt]# grep -n \\\'o\\\\{2\\\\}\\\' httpd.txt
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
17:# with ServerRoot set to \\\'/www\\\' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server\\\'s
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
28:# same ServerRoot for multiple httpd daemons, you will need to change at
31:ServerRoot /etc/httpd
54:# LoadModule foo_module modules/mod_foo.so
60:# httpd as root initially and it will switch.
63:# It is usually good practice to create a dedicated user and group for
86:ServerAdmin root@localhost
115:# DocumentRoot: The directory out of which you will serve your
119:DocumentRoot /var/www/html
130:# Further relax access to the default document root:
226: # Redirect permanent /foo http://www.example.com/bar
230: # access content that does not live under the DocumentRoot.
332:#ErrorDocument 500 The server made a boo boo.
358:wood
359:woooood
(2)查詢以 w 開頭以 d 結尾,中間包含 2~5 個 o 的字符串。
[root@localhost opt]# grep -n \\\'o\\\\{2,5\\\\}\\\' httpd.txt
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
17:# with ServerRoot set to \\\'/www\\\' will be interpreted by the
22:# ServerRoot: The top of the directory tree under which the server\\\'s
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
28:# same ServerRoot for multiple httpd daemons, you will need to change at
31:ServerRoot /etc/httpd
54:# LoadModule foo_module modules/mod_foo.so
60:# httpd as root initially and it will switch.
63:# It is usually good practice to create a dedicated user and group for
86:ServerAdmin root@localhost
115:# DocumentRoot: The directory out of which you will serve your
119:DocumentRoot /var/www/html
130:# Further relax access to the default document root:
226: # Redirect permanent /foo http://www.example.com/bar
230: # access content that does not live under the DocumentRoot.
332:#ErrorDocument 500 The server made a boo boo.
358:wood
359:woooood
[root@localhost opt]# egrep -n \\\'if|is|on\\\' httpd.txt
2:# This is the main Apache HTTP server configuration file. It contains the
3:# configuration directives that give the server its instructions.
4:# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
7:# for a discussion of each configuration directive.
9:# Do NOT simply read the instructions in here without understanding
10:# what they do. They\\\'re here only as hints or reminders. If you are unsure
11:# consult the online docs. You have been warned.
13:# Configuration and logfile names: If the filenames you specify for many
14:# of the server\\\'s control files begin with / (or drive:/ for Win32), the
16:# with /, the value of ServerRoot is prepended -- so \\\'log/access_log\\\'
23:# configuration, error, and log files are kept.
26:# ServerRoot at a non-local disk, be sure to specify a local disk on the
27:# Mutex directive, if file-based mutexes are used. If you wish to share the
...//省略部分內容...