Linux学习----grep及正则表达式

来源:互联网 发布:mysql执行sql文件 最快 编辑:程序博客网 时间:2024/05/17 23:12

grep: Global Reaserch

根据模式,搜索文本,并将符合模式的文本行显示出来。
还有egrep、fgrep

模式:Pattern  文本字符和正则表达式的元字符组合而成的匹配条件。

NAME
       grep, egrep, fgrep - print lines matching a pattern


SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]


[root@localhost ~]# grep 'root' /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin

-i : --ignore-case   忽略大小写
--colour[=WHEN] : 匹配到的内容加上颜色显示
-v :反向查找。被模式匹配到的行不显示,显示没有被匹配到的行。
-o:只显示被匹配到的字符串
[root@localhost ~]# grep 'root' /etc/passwd -orootrootrootroot

*:任意长度的任意字符
?:任意单个字符
[]: 范围
[^]:


正则表达式:Regular EXPression

默认情况下,正则表达式工作在贪婪模式下----尽可能长的匹配。
元字符:
    . :任意单个字符
[root@localhost ~]# grep 'r..t' /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinftp:x:14:50:FTP User:/va<span style="color:#ff0000;">r/ft</span>p:/sbin/nologin
    []: 匹配指定范围内的任意单个字符
    [^]:匹配指定范围外的任意单个字符
            字符集和:[:digit:]    [:lower:]  [:upper:]   [:space:]  


字符次数:
    * :匹配其前面的字符任意次
   .*:任意长度,任意字符

 \? :匹配其前面的字符一次或0次
[root@localhost test]# grep 'a\?b' test<span style="color:#ff0000;">abb</span>aa<span style="color:#ff0000;">ab</span>amn<span style="color:#ff0000;">b</span>ad<span style="color:#ff0000;">b</span>

\{m,n\} :匹配之前的字符至少m次,至多n次
[root@localhost test]# grep 'a\{1,3\}b' testabaaab

[root@localhost test]# grep 'a.\{1,3\}b' testaaabamnbadb

位置锚定:
^: 锚定行首。此字符后面的任意内容必须出现在行首
[root@localhost test]# grep '^r..t' /etc/passwdroot:x:0:0:root:/root:/bin/bash

$:锚定行尾。此字符前面的任意内容必须出现在行尾
[root@localhost test]# grep '3$' /etc/inittab # multi-user.target: analogous to runlevel 3
^$: 空白行
[root@localhost test]# grep '[[:digit:]]$' /etc/inittab # multi-user.target: analogous to runlevel 3# graphical.target: analogous to runlevel 5

锚定词首:
\< 或\b:其后面的任意字符必须作为单词的首部出现
\> 或\b:其前面的任意字符必须作为单词的尾部出现

\<root\> 
root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin

分组:
\(\): 将内容分组
     \(ab\)*:ab匹配的次数
    后向引用。分组内容可以在后续用一个字符继续引用。
    \1: 调用前面第一个左括号以及与之对应的右括号所包括的所有内容
[root@localhost test]# grep 'l..e' t2.txt He love his lover.She like her liker.He like his lover.

[root@localhost test]# grep '\(l..e\).*\1' t2.txt He love his lover.She like her liker.



扩展正则表达式:Extended REGEXP

grep  -E  使用扩展正则表达式
          -A  n:显示匹配到的一行,及之后的n行
          
[root@localhost ~]# grep  -A 2 '^core id' /proc/cpuinfo core id: 0cpu cores: 2apicid: 0--core id: 1cpu cores: 2apicid: 1--core id: 0cpu cores: 2apicid: 2--core id: 1cpu cores: 2apicid: 3

       -B n: 及其之前的n行
       -C n: 及其前后各n行

扩展正则表达式:
字符匹配:
.
[]
[^]

次数匹配:
?
+:其前面的字符至少一次
{m,n}

位置锚定:
^
$
\<
\>

分组:
()
\1,\2.....

或者:
|: or  或者
      
[root@localhost ~]# grep -E 'C|cat' t3.txt <span style="color:#ff0000;">cat</span><span style="color:#ff0000;">C</span>at<span style="color:#ff0000;">C</span>China

[root@localhost ~]# grep -E '(C|c)at' t3.txt catCat

[root@localhost ~]# ifconfig | egrep '\b([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b'        inet <span style="color:#ff0000;">192</span>.<span style="color:#ff0000;">168</span>.<span style="color:#ff0000;">1</span>.<span style="color:#ff0000;">70</span>  netmask <span style="color:#ff0000;">255</span>.<span style="color:#ff0000;">255</span>.<span style="color:#ff0000;">255</span>.0  broadcast <span style="color:#ff0000;">192.168.1.255</span>        inet6 fe80::20c:29ff:fe0c:4888  prefixlen <span style="color:#ff0000;">64</span>  scopeid 0x20<link>        ether 00:0c:29:0c:<span style="color:#ff0000;">48:88</span>  txqueuelen 1000  (Ethernet)        RX packets 23044  bytes 1640772 (<span style="color:#ff0000;">1.5</span> MiB)        TX packets 5501  bytes 962523 (939.<span style="color:#ff0000;">9</span> KiB)

[root@localhost ~]# ifconfig | egrep  '(\b([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\.){3}\b([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b'        inet <span style="color:#ff0000;">192.168.1.70</span>  netmask <span style="color:#ff0000;">255.255.255.0</span>  broadcast <span style="color:#ff0000;">192.168.1.255</span>        inet <span style="color:#ff0000;">127.0.0.1</span>  netmask <span style="color:#ff0000;">255.0.0.0</span>        inet <span style="color:#ff0000;">192.168.122.1</span>  netmask 255.255.255.0  broadcast <span style="color:#ff0000;">192.168.122.255</span>



fgrep:fast  不支持正则表达式




























1 0
原创粉丝点击