【每天一个Linux命令】17. 强大的文件搜索工具grep&&正则表达式

前端之家收集整理的这篇文章主要介绍了【每天一个Linux命令】17. 强大的文件搜索工具grep&&正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#新建一个测试文件,下面会用到

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ cat testgrep
wirelessqa is my blog
wirelestqa is testfile
wirelesmqa is testfile
mmmqa is testfile
mmqa
mqa is testfile
Wirelessqa is my blog
my blog is wirelessqa
my name is bixiaopeng
bixiaopeng is my name
bxp is my name
my name is bxp
my name is (bxp)
b$##

1. 匹配行的开始^


#例:'^wirelessqa'匹配所有以wirelessqa开头的行
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e ^wirelessqa testgrep
wirelessqa is my blog
#或
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e ^[wirelessqa] testgrep
wirelessqa is my blog

2. 匹配定行的结束$

#例:'wirelessqa$'匹配所有以wirelessqa结尾的行
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e wirelessqa$ testgrep
my blog is wirelessqa

3. 匹配一个非换行符的字符.

#例:'wireles.qa'匹配wireles后接一个任意字符,然后是qa
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e wireles.qa testgrep
wirelessqa is my blog
wirelestqa is testfile
wirelesmqa is testfile
my blog is wirelessqa

4. 匹配零个或多个先前字符*

#例1:' *qa'匹配所有一个或多个空格后紧跟qa的行
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e m*qa testgrep
wirelessqa is my blog
wirelestqa is testfile
wirelesmqa is testfile
mmmqa is testfile
mqa is testfile
Wirelessqa is my blog
my blog is wirelessqa
#例2: .*一起用代表任意字符
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e .*qa testgrep
wirelessqa is my blog
wirelestqa is testfile
wirelesmqa is testfile
mmmqa is testfile
mqa is testfile
Wirelessqa is my blog
my blog is wirelessqa

5. 匹配一个指定范围内的字符[]

#例1: '[Ww]irelessqa'匹配Wirelessqa和wirelessqa
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e [Ww]irelessqa testgrep
wirelessqa is my blog
Wirelessqa is my blog
my blog is wirelessqa

#例子2:[m*qa]匹配以m开头的字符或以qa结尾包含一个或多个m的字符
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e [m*qa] testgrep
wirelessqa is my blog
wirelestqa is testfile
wirelesmqa is testfile
mmmqa is testfile
mqa is testfile
Wirelessqa is my blog
my blog is wirelessqa
my name is bixiaopeng
bixiaopeng is my name
bxp is my name
my name is bxp

6. 匹配一个不在指定范围内的字符[^]

#例:'[^a-fh-m]qa'匹配不包含a-f和h-m的一个字母开头,紧跟qa的行
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e [^a-fh-m]qa testgrep
wirelessqa is my blog
wirelestqa is testfile
Wirelessqa is my blog
my blog is wirelessqa


7. 标记匹配字符\(..\)


#例:匹配包含'\(bxp\)'的行

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e \(bxp\) testgrep
my name is (bxp)

8.匹配单词的开始\<

例:'\<wire'匹配包含以wire开头的单词的行。

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e '\<wire' testgrep
wirelessqa is my blog
wirelestqa is testfile
wirelesmqa is testfile
my blog is wirelessqa

9.匹配单词的结束\>

#例:'blog\>'匹配包含以blog结尾的单词的行。
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'blog\>' testgrep
wirelessqa is my blog
Wirelessqa is my blog
my blog is wirelessqa

10. 连续重复字符x,m次x\{m\}

#例:'m\{3\}'匹配包含连续3个m的行。
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'm\{3\}' testgrep
mmmqa is testfile

11.连续重复字符x,至少m次x\{m,\}


#例:'m\{2,\}'匹配至少连续有2个m的行。
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'm\{2,\}' testgrep
mmmqa is testfile
mmqa 

12.连续重复字符x,至少m次,不多于n次

#例:'m\{2,3\}'匹配连续2--3个m的行
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'm\{2,3\}' testgrep
mmmqa is testfile
mmqa
mmmmqa

13.匹配一个文字和数字字符,也就是[A-Za-z0-9] \w

#例:'b\w*p'匹配以b后跟零个或多个文字或数字字符,然后是p。
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'b\w*p' testgrep
my name is bixiaopeng
bixiaopeng is my name
bxp is my name
my name is bxp
my name is (bxp)

14.w的反置形式,匹配一个非单词字符\W

#例: 匹配b后面为非[A-Za-z0-9]的行
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'b\W' testgrep
b$##

15.单词锁定符\b

#例: '\bmqa\b'只匹配mqa,即只能是mqa这个单词,两边均为空格。
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e '\bmqa\b' testgrep
mqa is testfile

#单个\b也可以用,结果如下:
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e '\mqa\b' testgrep
wirelesmqa is testfile
mmmqa is testfile
mmqa
mqa is testfile

16. 只包某个字符的行 ^字符$

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e '^mmqa$' testgrep
mmqa

17.用户用户组及其他用户组成员有可执行权限的目录^d..x..x. .x

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al | grep '^d..x..x..x'
drwxr-xr-x+ 92 bixiaopeng  staff    3128 Sep 21 15:33 .
drwxr-xr-x   5 root        admin     170 Jul 27 19:39 ..
drwxr-xr-x   8 bixiaopeng  staff     272 Jul 18 18:24 .Genymobile
drwxr-xr-x   4 bixiaopeng  staff     136 Dec 12  2012 .adobe
…...          

18. 只显示非目录的文件ls –l | grep ^[^d]

bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep ^[^d]
total 2856
-rw-------   1 bixiaopeng  staff       5 Dec  8  2012 .CFUserTextEncoding
-rw-r--r--@  1 bixiaopeng  staff   39940 Sep 21 10:22 .DS_Store
-rw-------   1 bixiaopeng  staff     147 Jun 18 09:20 .Xauthority
-rw-r--r--   1 bixiaopeng  staff   12288 Jul 17 23:24 .bad.txt.swp

猜你在找的正则表达式相关文章