加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 02:07:46 所属栏目:百科 来源:网络整理
导读:#新建一个测试文件,下面会用到 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ cat testgrepwirelessqa is my blogwirelestqa is testfilewirelesmqa is testfilemmmqa is testfilemmqamqa is testfileWirelessqa is my blogmy blog is wirelessqamy name is bix

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

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

#例:'bw*p'匹配以b后跟零个或多个文字或数字字符,然后是p。
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'bw*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 'bW' testgrep
b$##

15.单词锁定符b

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

#单个b也可以用,结果如下:
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -e 'mqab' 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读