基礎正則表達式
擴展正則表達式
正則表達式概述
1. 正則表達式的定義
正則表達式又稱正規表達式、常規表達式。在代碼中常簡寫為 regex、regexp或 RE;正則表達式是使用單個字符串來描述、匹配一系列符合某個句法規則的字符串簡單來說, 是一種匹配字符串的方法,通過一些特殊符號,實現快速查找、刪除、替換某個特定字符串。
正則表達式是由普通字符與元字符組成的文字模式,普通字符包括大小寫字母、數字、標點符號及一些其他符號,元字符則是指那些在正則表達式中具有特殊意義的專用字符,可以用來規定其前導字符(即位于元字符前面的字符)在目標對象中的出現模式。
正則表達式一般用于腳本編程與文本編輯器中。
2.正則表達式用途
正則表達式對于系統管理員來說是非常重要的,系統運行過程中會產生大量的信息,這些信息有些是非常重要的,有些則僅是告知的信息。身為系統管理員如果直接看這么多的信息數據,無法快速定位到重要的信息,如“用戶賬號登錄失敗”“服務啟動失敗”等信息。這時可以通過正則表達式快速提取“有問題”的信息。如此一來,可以將運維工作變得更加簡單、方便。
基礎正則表達式
正則表達式的字符串表達方法根據不同的嚴謹程度與功能分為基本正則表達式與擴展正則表達式。基礎正則表達式是常用的正則表達式的最基礎的部分。在 Linux 系統中常見的文件處理工具中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”表示不區分大小寫
[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
...//省略部分內容...
2) 利用中括號“[]”來查找集合字符
在httpd.txt測試文件中添加字符串shirt、short、wd、wod、wood、woooood。想要查找“shirt”與“short”這兩個字符串時,可以發現這兩個字符串均包含“sh” 與“rt”。此時執行以下命令即可同時查找到“shirt”與“short”這兩個字符串。“[]”中無論有幾個字符,都僅代表一個字符,也就是說“[io]”表示匹配“i”或者“o”。
[root@localhost opt]# vim httpd.txt
...//省略部分內容...
# Supplemental configuration
#
# Load config files in the /etc/httpd/conf.d directory, if any.
IncludeOptional conf.d/*.conf
shirt
short
wd
wod
wood
woooood
:wq
[root@localhost opt]# grep -n \\\'sh[io]rt\\\' httpd.txt
354:shirt
355:short
若要查找包含重復單個字符“oo”時,只需要執行以下命令即可。
[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
在上述命令的執行結果中發現“woooood”也符合匹配規則,上述結果中可以得知,“oo”前面的“o”是符合匹配規則的。若不希望“oo”前面存在小寫字母,可以使用“grep –n‘[^a-z]oo’httpd.txt”命令實現,其中“a-z”表示小寫字母,大寫字母則通過“A-Z”表示。
[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
3) 查找行首“^”與行尾字符“$”
基礎正則表達式包含兩個定位元字符:“^”(行首)與“$”(行尾)。若想查找 “Ser”字符串為行首的行,則可以通過“^”元字符來實現。
[root@localhost opt]# grep -n \\\'^Ser\\\' httpd.txt
31:ServerRoot /etc/httpd
86:ServerAdmin root@localhost
查詢以小寫字母開頭的行可以通過“^[a-z]”規則來過濾,查詢大寫字母開頭的行則使用“^[A-Z]”規則,若查詢不以字母開頭的行則使用“^[^a-zA-Z]”規則。“^”符號在元字符集合“[]”符號內外的作用是不一樣的,在“[]”符號內表示反向選擇,在“[]”符號外則代表定位行首。
[root@localhost opt]# grep -n \\\'^[a-z]\\\' httpd.txt
354:shirt
355:short
356:wd
357:wod
358:wood
359:woooood
[root@localhost opt]# grep -n \\\'^[A-Z]\\\' httpd.txt
31:ServerRoot /etc/httpd
42:Listen 80
56:Include conf.modules.d/*.conf
66:User apache
67:Group apache
86:ServerAdmin root@localhost
119:DocumentRoot /var/www/html
182:ErrorLog logs/error_log
189:LogLevel warn
316:AddDefaultCharset UTF-8
348:EnableSendfile on
353:IncludeOptional conf.d/*.conf
[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.
...//省略部分內容...
當查詢空白行時,執行“grep –n ‘^$’ httpd.txt”命令即可。
[root@localhost opt]# grep -n \\\'^$\\\' httpd.txt
20:
32:
43:
57:
68:
80:
87:
96:
...//省略部分內容...
4) 查找任意一個字符“.”與重復字符“*”
在正則表達式中小數點(.)也是一個元字符,代表任意一個字符。例如, 執行以下命令就可以查找“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”匹配規則。若想要查詢o、oo、ooooo 等資料,則需要使用星號(*)元字符。但需要注意的是,“*”代表的是重復零個或多個前面的單字符。“o*”表示擁有零個(即為空字符)或大于等于一個“o”的字符,因為允許空字符,所以執行“grep –n‘o*’ httpd.txt”命令會將文本中所有的內容都輸出打印。如果是“oo*”, 則第一個 o 必須存在,第二個 o 則是零個或多個 o,所以凡是包含 o、oo、ooooo,等的資料都符合標準。同理,若查詢包含至少兩個 o 以上的字符串,則執行“grep –n‘ooo*’ httpd.txt”命令即可。
[root@localhost opt]# grep -n \\\'o*\\\' httpd.txt
...//省略部分內容...
353:IncludeOptional conf.d/*.conf
354:shirt
355:short
356:wd
357:wod
358:wood
359:woooood
[root@localhost opt]# grep -n \\\'oo*\\\' httpd.txt
...//省略部分內容...
353:IncludeOptional conf.d/*.conf
355:short
357:wod
358:wood
359:woooood
[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
查詢以 w 開頭 d 結尾,中間包含至少一個 o 的字符串,執行以下命令即可實現。
[root@localhost opt]# grep -n \\\'woo*d\\\' httpd.txt
357:wod
358:wood
359:woooood
查詢以 w 開頭 d 結尾,中間的字符可有可無的字符串。
[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
5) 查找連續字符范圍“{}”
在上面的示例中,我們使用“.”與“*”來設定零個到無限多個重復的字符,如果想要限制一個范圍內的重復的字符串,這個時候就需要使用基礎正則表達式中的限定范圍的字符“{}”,因為“{}”在 Shell 中具有特殊 意義,所以在使用“{}”字符時,需要利用轉義字符“\\\\”,將“{}”字符轉換成普通字符。
(1)查詢兩個 o 的字符。
[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
(3)查詢以 w 開頭以 d 結尾,中間包含 2 以上 o 的字符串。
[root@localhost opt]# grep -n \\\'wo\\\\{2\\\\}\\\' httpd.txt
358:wood
359:woooood
2. 元字符總結
^
匹配輸入字符串的開始位置。除非在方括號表達式中使用,表示不包含該字符集合。要匹配“^”字符本身,請使用“\\\\^”
$
匹配輸入字符串的結尾位置。如果設置了 RegExp 對象的 Multiline 屬性,則“$”也匹配‘\\\\n’或‘\\\\r’。要匹配“$”字符本身,請使用“\\\\$”
.
匹配除“\\\\r\\\\n”之外的任何單個字符
\\\\
將下一個字符標記為特殊字符、原義字符、向后引用、八進制轉義符。例如,‘n’匹配字符“n”。 ‘\\\\n’匹配換行符。序列‘\\\\\\\\’匹配“\\\\”,而‘\\\\(’則匹配“(”
*
匹配前面的子表達式零次或多次。要匹配“*”字符,請使用“\\\\*”
[]
字符集合。匹配所包含的任意一個字符。例如,“[abc]”可以匹配“plain”中的“a”
[^]
賦值字符集合。匹配未包含的一個任意字符。例如,“[^abc]”可以匹配“plain”中“plin”中的任何一個字母
[n1-n2]
字符范圍。匹配指定范圍內的任意一個字符。例如,“[a-z]”可以匹配“a”到“z”范圍內的任意一個小寫字母字符。注意:只有連字符(-)在字符組內部,并且出現在兩個字符之間時,才能表示字符的范圍;如果出現在字符組的開頭,則只能表示連字符本身
{n}
n 是一個非負整數,匹配確定的n 次。例如,“o{2}”不能匹配“Bob”中的“o”,但是能匹配“food”中的兩個o
{n,}
n 是一個非負整數,至少匹配n 次。例如,“o{2,}”不能匹配“Bob”中的“o”,但能匹配“foooood”中的所有 o。“o{1,}”等價于“o ”。“o{0,}”則等價于“o*”
{n,m}
m 和n 均為非負整數,其中 n<=m,最少匹配 n 次且最多匹配 m 次
擴展正則表達式
通常情況下會使用基礎正則表達式就已經足夠了,但有時為了簡化整個指令,需要使用范圍更廣的擴展正則表達式,例如,使用基礎正則表達式查詢除文件中空白行與行首為“#”之外的行(通常用于查看生效的配置文件),執行“grep –v ‘^$’ httpd.txt | grep –v ‘^#’”即可實現。這里需要使用管道命令來搜索兩次。如果使用擴展正則表達式,可以簡化為“egrep –v ‘^$|^#’ httpd.txt”,其中,單引號內的管道符號表示或者(or)。
[root@localhost opt]# grep -v \\\'^$\\\' httpd.txt | grep -v ^#
ServerRoot /etc/httpd
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
...//省略部分內容...
[root@localhost opt]# egrep -v \\\'^$|^#\\\' httpd.txt
ServerRoot /etc/httpd
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot /var/www/html
<Directory /var/www>
AllowOverride None
# Allow open access:
Require all granted
</Directory>
...//省略部分內容...
grep 命令僅支持基礎正則表達式,如果使用擴展正則表達式,需要使用 egrep 或 awk 命令。awk 命令在后面進行講解,這里我們直接使用 egrep 命令。egrep命令與 grep 命令的用法基本相似。egrep 命令是一個搜索文件獲得模式,使用該命令可以搜索文件中的任意字符串和符號,也可以搜索一個或多個文件的字符串,一個提示符可以是單個字符、一個字符串、一個字或一個句子。
與基礎正則表達式類型相同,擴展正則表達式也包含多個元字符,常見的擴展正則表達式的元字符主要包括以下幾個:
重復一個或者一個以上的前一個字符
?
零個或者一個的前一個字符
|
使用或者(or)的方式找出多個字符
()
查找“組”字符串
()
辨別多個重復的組
示例
執行“egrep -n \\\'wo d\\\' httpd.txt”命令,即可查詢wod、wood、woooood等字符串。
[root@localhost opt]# egrep -n \\\'wo d\\\' httpd.txt
357:wod
358:wood
359:woooood
執行“egrep -n \\\'wo?d\\\' httpd.txt”命令,即可查詢“wd”“wod”這兩個字符串。
[root@localhost opt]# egrep -n \\\'wo?d\\\' httpd.txt
168:# The following lines prevent .htaccess and .htpasswd files from being
356:wd
357:wod
執行“egrep -n \\\'of|is|on\\\' httpd.txt”命令即可查詢of或者if或者on字符串。
[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
...//省略部分內容...
“egrep -n \\\'sh(i|o)rt\\\' httpd.txt”。`“shirt”與“short”因為這兩個單詞的“sh”與“rt”是重復的,所以將“i”與“o列于“()”符號當中,并以“|”分隔,即可查詢shirt或者short字符串。
[root@localhost opt]# egrep -n \\\'sh(i|o)rt\\\' httpd.txt
354:shirt
355:short
“egrep -n \\\'A(xyz) C\\\' httpd.txt”。該命令是查詢開頭的A結尾是C,中間有一個以上的xyz字符串的意思,在httpd.txt文件中添加字符串AxyzC、AxyzxyzC。
[root@localhost opt]# vim httpd.txt
...//省略部分內容...
woooood
AxyzC
AxyzxyzC
~
~
:wq
[root@localhost opt]# egrep -n \\\'A(xyz) C\\\' httpd.txt
360:AxyzC
361:AxyzxyzC
更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn