正则表达式及Linux文本检查工具

首先大家要通晓哪些是正则表明式?
   
用最简便的话来讲,正则表明式正是一套为了管理大量的字符串来定义的某种法则和方式;或许换一句话来说,正则表明式正是用部分例外的字符来重新定义表示含义:
比如说:大家把”.”表示放肆的单个字符;这样的类似的重复定义就是我们讲的正则表明式;
 
正则表明式布满的援用在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会依据标准输入的“PATTESportageN”或许被取名的文书找寻相应的行,暗许情形下会打字与印刷相配的行
[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
杰森:x:一千: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:
总计找到‘PATTE景逸SUVN’的次数
[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: PATTECRUISERN, –regexp=PATTEENCOREN:多方式机制;
-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.htm

Linux正则表明式sed 详述 
http://www.linuxidc.com/Linux/2015-04/116309.htm

Linux正则表明式特性及BRE与ERE的差异
http://www.linuxidc.com/Linux/2014-03/99152.htm

grep使用简明及正则表明式
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.htm

Linux中正则表明式与公事格式化管理命令(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:] : 表示键盘上面的支配按钮即包括”CEnclave,LF,Tab,Del”
[:print:] : 代表能够打字与印刷出来的字符
[:xdigit:] :代表十六进制的数字类型->”0-9,A-F,a-f”
下面八个不日常利用,就不演示了

更加的多实际情况见请继续阅读下一页的出色内容
http://www.linuxidc.com/Linux/2016-03/129298p2.htm

永利澳门游戏网站 1

发表评论

电子邮件地址不会被公开。 必填项已用*标注