正则表达式及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会根据标准输入的“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正则表达式;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/15419.html