首先我们要明白什么是正则表达式?
用最简单的话来说,正则表达式就是一套为了处理大量的字符串来定义的某种规则和方法;或者换一句话来讲,正则表达式就是用一些特殊的字符来重新定义表示含义:
例如:我们把"."表示任意的单个字符;这样的类似的重新定义就是我们讲的正则表达式;
正则表达式广泛的引用在grep工具中,所以我们先通过grep慢慢引出什么是正则表达式...一、linux正则表达式之前的三个文本查找命令
grep:(global search regular RE )全面搜索正则表达式并把行打印出来)相关解释:最早的文本匹配程序,使用POSIX定义的基本正则表达式(BRE)来匹配文本
名称:print lines matching a pattern是一种强大的文本搜索工具,它只能使用基本的正则表达式来搜索文本,并把匹配的行打印出来
[root@linux ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@linux ~]# 格式:
1)grep [OPTIONS] PATTERN [FILE...]
###########下面我们就根据这个文件进行讲解###########
[root@linux ~]# cat test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?
######################################################################################### 2)grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
描述:grep会根据标准输入的“PATTERN”或者被命名的文件搜索相应的行,默认情况下会打印匹配的行
[root@linux ~]# grep "telphone" test.txt
My telphone number is 15648562351...
[root@linux ~]# 常用选项:
-E: 相当于egrep,是由POSIX指定,利用此命令可以使用扩展的正则表达式对文本进行搜索,并把符合用户需求的字符串打印出来
注意:当我们使用egrep的时候我们就不需要对特殊的字符进行转移操作了,这一点与grep有一点差别:
先来看看egrep的使用:
123 [root@linux ~]# egrep "beautiful" test.txt
This is a beautiful girl
[root@linux ~]#下面是grep -E类似与egrep的功能
[root@linux ~]# grep -E "^(a|J)" /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
Jason:x:1000:1000::/home/Jason:/bin/bash-F: 相当于fgrep,是由Posix指定,它利用固定的字符串来对文本进行搜索,但不支持正则表达式的引用,所以此命令的执行速度也最快
[root@linux ~]# grep -F "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin--color=auto/nerver/always:对匹配到的文本着色后高亮显示,一般在alias中定义;
[root@linux ~]# alias
alias cp="cp -i"
alias egrep="egrep --color=auto"
alias fgrep="fgrep --color=auto"
alias grep="grep --color=auto"
alias l.="ls -d .* --color=auto"
alias ll="ls -l --color=auto"
alias ls="ls --color=auto"
alias mv="mv -i"
alias rm="rm -i"
alias which="alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde"
[root@linux ~]#[root@linux ~]# grep "home" --color=auto /etc/passwd
Jason:x:1000:1000::/home/Jason:/bin/bash
[root@linux ~]# grep "home" --color=never /etc/passwd
Jason:x:1000:1000::/home/Jason:/bin/bash
[root@linux ~]# grep "home" --color=always /etc/passwd
Jason:x:1000:1000::/home/Jason:/bin/bash
[root@linux ~]#-i:忽略字符大小写;
[root@linux ~]# cat test.txt
Good morning,zhang An!
[root@linux ~]# grep -i "a" test.txt
Good morning,zhang An!-o:仅显示匹配到的文本自身;
[root@linux ~]# cat test.txt
Good morning,zhang An!
[root@linux ~]# grep -o "zhang" test.txt
zhang
[root@linux ~]#-v: --invert-match:反向匹配,匹配引号之外的行
[root@linux ~]# cat test.txt
Good morning,zhang An!
nihao
[root@linux ~]# grep -v "Good" test.txt
nihao
[root@linux ~]#
#在这里可以看出反向匹配是打印出来不包含"Good"的行-q: --quiet, --silient:静默模式,不输出任何信息;
[root@linux ~]# grep -v "Good" test.txt
nihao
[root@linux ~]# grep -qv "Good" test.txt
[root@linux ~]#-n:显示匹配到行,并且显示行号
[root@linux ~]# grep -n "o" test.txt
1:Good morning,zhang An!
2:nihao
[root@linux ~]# grep "o" test.txt | cat -n
1 Good morning,zhang An!
2 nihao
[root@linux ~]#
#grep的n选项是有颜色的与cat的n选项有一些差别 -c: 计算找到‘PATTERN’的次数
[root@linux ~]# grep -c "o" test.txt
2
[root@linux ~]# -A:显示匹配到字符那行的后面n行
[root@linux ~]# cat test.txt
gegVDFwer34fs43dfwerFG4g
gegVDFweSDFGertgg
23ere67fgSD5436fe
nihao,zhandge
[root@linux ~]# grep -A1 "23" test.txt
23ere67fgSD5436fe
nihao,zhandge
[root@linux ~]# -B:显示匹配到字符那行的前面n行
[root@linux ~]# cat test.txt
gegVDFwer34fs43dfwerFG4g
gegVDFweSDFGertgg
23ere67fgSD5436fe
nihao,zhandge
[root@linux ~]# grep -B2 "23" test.txt
gegVDFwer34fs43dfwerFG4g
gegVDFweSDFGertgg
23ere67fgSD5436fe
[root@linux ~]#-C:显示匹配到字符那行的前后n行
[root@linux ~]# grep -C1 "23" test.txt
gegVDFweSDFGertgg
23ere67fgSD5436fe
nihao,zhandge
[root@linux ~]#-G:--basic-regexp:支持使用基本正则表达式;
-P:--perl-regexp:支持使用pcre正则表达式;-e: PATTERN, --regexp=PATTERN:多模式机制;
-f: FILE, --file=FILE:FILE为每行包含了一个pattern的文本文件,即grep script;
下面就不演示这两个,上面有相关的例子
egrep:扩展式grep,其使用扩展式正规表达式(ERE)来匹配文本。egrep命令等同于grep -E,利用此命令可以使用扩展的正则表达式对文本进行搜索,并把符合用户需求的字符串打印出来。
fgrep:快速grep,这个版本匹配固定字符串而非正则表达式。并且是唯一可以并行匹配多个字符串的版本。fgrep命令等同于grep -F,它利用固定的字符串来对文本进行搜索,但不支持正则表达式的引用,所以此命令的执行速度也最快。Linux 基础入门教程----正则表达式基础 http://www.linuxidc.com/Linux/2015-08/121441.htmLinux正则表达式sed 详述 http://www.linuxidc.com/Linux/2015-04/116309.htmLinux正则表达式特性及BRE与ERE的区别 http://www.linuxidc.com/Linux/2014-03/99152.htmgrep使用简明及正则表达式 http://www.linuxidc.com/Linux/2013-08/88534.htm正则表达式的用法 http://www.linuxidc.com/Linux/2013-03/81897.htm正则表达式之零宽断言 http://www.linuxidc.com/Linux/2013-03/81897.htmLinux中正则表达式与文件格式化处理命令(awk/grep/sed) http://www.linuxidc.com/Linux/2013-03/81018.htm基础正则表达式 http://www.linuxidc.com/Linux/2014-09/106296.htm常用正则表达式整理 http://www.linuxidc.com/Linux/2014-10/108076.htm二、基本正则表达式:
基本意义:由一些基本字符以及某些特殊字符搭配,组合成一段具有某种语法规则的能轻松搜索并匹配文本的字符串
分类:基本正则表达式与扩展正则表达式
1)基本正则表达式的元字符
什么是元字符?
元字符是一个或一组代替一个或多个字符的字符,其实呢就是下面的这几类.
1)字符匹配
.:表示匹配任意的单个字符
[root@linux ~]# grep "r..t" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@linux ~]#我们注意这样的一个例子:
1234567 [root@linux ~]# grep "." test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?这样就把所有的行都匹配出来了
[]:匹配指定范围内的单个字符:
[root@linux ~]# grep "[aj]h" test002.txt
ahjhb
[root@linux ~]#[^]:匹配指定范围内的单个字符
[root@linux ~]# grep "[^a]h" test002.txt
ahjhb
[root@linux ~]#[:alnum:] : 数字与字母大小写字符-->"A-Za-z0-9"
[root@linux ~]# grep "[[:alnum:]]" test002.txt
b
ab
acb
aaX2Ab
a[Ah?jhb
aba1baba5bab
[root@linux ~]#
####下面的我就不再一一在例子了,很简单[:digit:] : 数字字符-------------->"0-9"
[root@linux ~]# grep "[[:digit:]]" test.txt
My telphone number is 15648562351...
[root@linux ~]# 把电话号码匹配出来了[:punct:] : 标点符号字符---------->"?.,"
1234567 [root@linux ~]# grep "[[:punct:]]" test.txt
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?
[root@linux ~]# 把所有的标点符号匹配出来了[:alpha:] : 字母字符-------------->"A-Za-z"
12345678 [root@linux ~]# grep "[[:alpha:]]" test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?
除了字母是不是都过滤掉了?[:graph:] : 除空格符(空格键与(Tab)按键)外的其他所有按键
[root@linux ~]# grep "[[:graph:]]" test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?
[root@linux ~]# 看看前面的源文件,对比一下,是不是?[:space:] : 代表的是空白字符,包括空格键[Tab]等
[root@linux ~]# grep "[[:graph:]]" test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?
[root@linux ~]# 这个演示的效果不太明显,你可以试一试"grep "[^[:space:]]" test.txt"[:blank:] : 代表的是空格键与[Tab]按键
[:lower:] : 小写字母字符---------->"a-z"
[root@linux ~]# grep "[[:lower:]]" test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?[:upper:] : 大写字母字符---------->"A-Z"
[root@linux ~]# grep "[^[:lower:]]" test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
can you tell me your phone number?
My telphone number is 15648562351...
Beat wish to you ?这样写是不是也对呢?
[root@linux ~]# grep "[[:upper:]]" test.txt
This is a beautiful girl
So do you want to know who is her?
oh! I can`t tell you?
My telphone number is 15648562351...
Beat wish to you ?[:cntrl:] : 表示键盘上面的控制按键即包括"CR,LF,Tab,Del"
[:print:] : 代表可以打印出来的字符
[:xdigit:] :代表十六进制的数字类型->"0-9,A-F,a-f"
上面三个不经常使用,就不演示了
更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2016-03/129298p2.htm
Linux磁盘管理入门教程CentOS7最小化安装以后没有ifconfig这个命令的解决方案相关资讯 正则表达式
- JavaScript正则表达式详解 (08月11日)
- Java处理正则表达式特殊字符转义 (08月03日)
- Python 正则表达式基础 (04月09日)
| - Linux文本处理工具grep和正则表达 (08月09日)
- Linux正则表达式初入门 (07月27日)
- Python的正则表达式 (03月24日)
|
本文评论 查看全部评论 (0)