zoukankan      html  css  js  c++  java
  • find grep

    find "path"   -name/-type/-utime "string"  -exec   {};
    grep [option] "string" "path"
    l就是文件,r就是行
    在find中嵌套grep -l就是文件 -r只是行的内容
    直接在grep中 -l就是文件, -r是文件+行的内容




    在当前目录下所有文件中查找内容包含 string 的文件: find ./ -name "*" -exec grep "string" {} ; 注意:在最后不能加 print ,否则会出错. 在当前目录下所有文件中查找内容包含 string 的文件并列出字符所在的文件: find ./ -name "*" -exec grep -l "string" {} ; 在当前目录下 *.c 中查找内容包含 string 的文件并列出字符所在的文件的所在行(不显示文件名): find ./ -name "*.c" -exec grep -n "string" {} ; 在当前目录下所有文件中查找内容包含 string 的文件并列出字符所在的文件,所在行及所在行的内容: find ./ -name "*" -exec grep -n "string" ./ {} ; 使用 type 选项:f(普通文件) d(目录) l(符号文件) 如果要在 /etc 目录下查找所有的目录: find /etc -type d -print 如果要在 /etc 目录下查找 .svn 的目录: find /etc -name .svn -type d -print 为了在当前目录下查找除目录以外的所有类型的文件: find . ! -type d -print 为了在当前目录下查找所有的符号链接文件, 可以用: find . -type l -print 为了用 ls -l 命令列出所匹配到的文件, 可以把 ls -l 命令放在find命令的 -exec 选项中: find . -type f -exec ls -l {} ;

    find ./ -type f -exec grep -n "#include" ./ {} ;

    查看当前面目录下所有文件的grep

    
    
    
    
    
    grep [-acinv] [--color=auto] '搜寻字符串' filename
    选项与参数:
    -a :将 binary 文件以 text 文件的方式搜寻数据
    -c :计算找到 '搜寻字符串' 的次数
    -i :忽略大小写的不同,所以大小写视为相同
    -n :顺便输出行号
    -v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
    --color=auto :可以将找到的关键词部分加上颜色的显示喔!
    
    
    
    根据文件内容递归查找目录
    # grep ‘energywise’ *           #在当前目录搜索带'energywise'行的文件
    # grep -r ‘energywise’ *        #在当前目录及其子目录下搜索'energywise'行的文件+所在行的内容
    # grep -l ‘energywise’ *        #在当前目录及其子目录下搜索'energywise'行的文件
    
    
    
    字符类
    字符类的搜索:如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st' 存在~这个时候,我可以这样来搜寻:
    
    [root@www ~]# grep -n 't[ae]st' regular_express.txt
    8:I can't finish the test.
    9:Oh! The soup taste good.
    
    其实 [] 里面不论有几个字节,他都谨代表某『一个』字节, 所以,上面的例子说明了,我需要的字串是『tast』或『test』两个字串而已!
    
     
    
    字符类的反向选择 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下
    注意这个文件是文件,不是目录
    [root@www ~]# grep -n '[^g]oo' regular_express.txt
    2:apple is my favorite food.
    3:Football game is not use feet only.
    18:google is the best tools for search keyword.
    19:goooooogle yes!
    
    
    字符类的连续:再来,假设我 oo 前面不想要有小写字节,所以,我可以这样写 [^abcd....z]oo , 但是这样似乎不怎么方便,由於小写字节的 ASCII 上编码的顺序是连续的, 因此,我们可以将之简化为底下这样:
    
    [root@www ~]# grep -n '[^a-z]oo' regular_express.txt
    3:Football game is not use feet only.
    
    
    行首与行尾字节 ^ $
    行首字符:如果我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!我们可以这样做:
    
    [root@www ~]# grep -n '^the' regular_express.txt
    12:the symbol '*' is represented as start.
     
    
    
    此时,就只剩下第 12 行,因为只有第 12 行的行首是 the 开头啊~此外, 如果我想要开头是小写字节的那一行就列出呢?可以这样:
    
    [root@www ~]# grep -n '^[a-z]' regular_express.txt
    2:apple is my favorite food.
    4:this dress doesn't fit me.
    10:motorcycle is cheap than car.
    12:the symbol '*' is represented as start.
    18:google is the best tools for search keyword.
    19:goooooogle yes!
    20:go! go! Let's go.
    
     
    
    如果我不想要开头是英文字母,则可以是这样:
    
    [root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt
    1:"Open Source" is a good mechanism to develop programs.
    21:# I am VBird
    ^ 符号,在字符类符号(括号[])之内与之外是不同的! 在 [] 内代表『反向选择』,在 [] 之外则代表定位在行首的意义!
    
    
    
    
    
    
    
    找出空白行:
    
    [root@www ~]# grep -n '^$' regular_express.txt
    22:
    因为只有行首跟行尾 (^$),所以,这样就可以找出空白行啦!
    
    
    任意一个字节 . 与重复字节 *
    这两个符号在正则表达式的意义如下:
    
    . (小数点):代表『一定有一个任意字节』的意思;
    * (星号):代表『重复前一个字符, 0 到无穷多次』的意思,为组合形态
    假设我需要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我可以这样做:
    
    [root@www ~]# grep -n 'g..d' regular_express.txt
    1:"Open Source" is a good mechanism to develop programs.
    9:Oh! The soup taste good.
    16:The world <Happy> is the same with "glad".
    因为强调 g 与 d 之间一定要存在两个字节,因此,第 13 行的 god 与第 14 行的 gd 就不会被列出来啦!
    
     
    
    
    
    如果我想要列出有 oo, ooo, oooo 等等的数据, 也就是说,至少要有两个(含) o 以上,该如何是好?
    
    因为 * 代表的是『重复 0 个或多个前面的 RE 字符』的意义, 因此,『o*』代表的是:『拥有空字节或一个 o 以上的字节』,因此,『 grep -n 'o*' regular_express.txt 』将会把所有的数据都列印出来终端上!
    
    当我们需要『至少两个 o 以上的字串』时,就需要 ooo* ,亦即是:
    [root@www ~]# grep -n 'ooo*' regular_express.txt
    1:"Open Source" is a good mechanism to develop programs.
    2:apple is my favorite food.
    3:Football game is not use feet only.
    9:Oh! The soup taste good.
    18:google is the best tools for search keyword.
    19:goooooogle yes!
    
    
    如果我想要字串开头与结尾都是 g,但是两个 g 之间仅能存在至少一个 o ,亦即是 gog, goog, gooog.... 等等,那该如何?
    [root@www ~]# grep -n 'goo*g' regular_express.txt
    18:google is the best tools for search keyword.
    19:goooooogle yes!
    
    
    
    如果我想要找出 g 开头与 g 结尾的行,当中的字符可有可无
    [root@www ~]# grep -n 'g.*g' regular_express.txt
    1:"Open Source" is a good mechanism to develop programs.
    14:The gd software is a library for drafting programs.
    18:google is the best tools for search keyword.
    19:goooooogle yes!
    20:go! go! Let's go.
    
    
    
    如果我想要找出『任意数字』的行?因为仅有数字,所以就成为:
    
    [root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt
    5:However, this dress is about $ 3183 dollars.
    15:You are the best is mean you are the no. 1.
    
    
    
    
    
    
    
    
    限定连续 RE 字符范围 {}
    我们可以利用 . 与 RE 字符及 * 来配置 0 个到无限多个重复字节, 那如果我想要限制一个范围区间内的重复字节数呢?
    
    举例来说,我想要找出两个到五个 o 的连续字串,该如何作?这时候就得要使用到限定范围的字符 {} 了。 但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符    来让他失去特殊意义才行。 至於 {} 的语法是这样的,假设我要找到两个 o 的字串,可以是:
    
    
    [root@www ~]# grep -n 'o{2}' regular_express.txt
    1:"Open Source" is a good mechanism to develop programs.
    2:apple is my favorite food.
    3:Football game is not use feet only.
    9:Oh! The soup taste good.
    18:google is the best tools for search ke
    19:goooooogle yes!
    
     
    
    假设我们要找出 g 后面接 25 个 o ,然后再接一个 g 的字串,他会是这样:
    
    [root@www
    ~]# grep -n 'go{2,5}g' regular_express.txt 18:google is the best tools for search keyword. 如果我想要的是 2 个 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是: [root@www ~]# grep -n 'go{2,}g' regular_express.txt 18:google is the best tools for search keyword. 19:goooooogle yes!

    在当前目录下所有文件中查找内容包含 string 的文件:find ./ -name "*" -exec grep "string" {} ;注意:在最后不能加 print ,否则会出错. 在当前目录下所有文件中查找内容包含 string 的文件并列出字符所在的文件:find ./ -name "*" -exec grep -l "string" {} ; 在当前目录下 *.c 中查找内容包含 string 的文件并列出字符所在的文件的所在行(不显示文件名):find ./ -name "*.c" -exec grep -n "string" {} ; 在当前目录下所有文件中查找内容包含 string 的文件并列出字符所在的文件,所在行及所在行的内容:find ./ -name "*" -exec grep -n "string" ./ {} ;
    使用 type 选项:f(普通文件) d(目录) l(符号文件)如果要在 /etc 目录下查找所有的目录:find /etc -type d -print 如果要在 /etc 目录下查找 .svn 的目录:find /etc -name .svn -type d -print
    为了在当前目录下查找除目录以外的所有类型的文件:find . ! -type d -print 为了在当前目录下查找所有的符号链接文件, 可以用:find . -type l -print

    为了用 ls -l 命令列出所匹配到的文件, 可以把 ls -l 命令放在find命令的 -exec 选项中:find . -type f -exec ls -l {} ;





    grep [-acinv] [--color=auto] '搜寻字符串' filename选项与参数:-a :将 binary 文件以 text 文件的方式搜寻数据-c :计算找到 '搜寻字符串' 的次数-i :忽略大小写的不同,所以大小写视为相同-n :顺便输出行号-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!--color=auto :可以将找到的关键词部分加上颜色的显示喔!


    根据文件内容递归查找目录# grep ‘energywise’ *           #在当前目录搜索带'energywise'行的文件# grep -r ‘energywise’ *        #在当前目录及其子目录下搜索'energywise'行的文件# grep -l -r ‘energywise’ *     #在当前目录及其子目录下搜索'energywise'行的文件,但是不显示匹配的行,只显示匹配的文件


    字符类字符类的搜索:如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st' 存在~这个时候,我可以这样来搜寻:
    [root@www ~]# grep -n 't[ae]st' regular_express.txt8:I can't finish the test.9:Oh! The soup taste good.
    其实 [] 里面不论有几个字节,他都谨代表某『一个』字节, 所以,上面的例子说明了,我需要的字串是『tast』或『test』两个字串而已!
     
    字符类的反向选择 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下注意这个文件是文件,不是目录[root@www ~]# grep -n '[^g]oo' regular_express.txt2:apple is my favorite food.3:Football game is not use feet only.18:google is the best tools for search keyword.19:goooooogle yes!

    字符类的连续:再来,假设我 oo 前面不想要有小写字节,所以,我可以这样写 [^abcd....z]oo , 但是这样似乎不怎么方便,由於小写字节的 ASCII 上编码的顺序是连续的, 因此,我们可以将之简化为底下这样:
    [root@www ~]# grep -n '[^a-z]oo' regular_express.txt3:Football game is not use feet only.

    行首与行尾字节 ^ $行首字符:如果我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!我们可以这样做:
    [root@www ~]# grep -n '^the' regular_express.txt12:the symbol '*' is represented as start. 

    此时,就只剩下第 12 行,因为只有第 12 行的行首是 the 开头啊~此外, 如果我想要开头是小写字节的那一行就列出呢?可以这样:
    [root@www ~]# grep -n '^[a-z]' regular_express.txt2:apple is my favorite food.4:this dress doesn't fit me.10:motorcycle is cheap than car.12:the symbol '*' is represented as start.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.
     
    如果我不想要开头是英文字母,则可以是这样:
    [root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt1:"Open Source" is a good mechanism to develop programs.21:# I am VBird^ 符号,在字符类符号(括号[])之内与之外是不同的! 在 [] 内代表『反向选择』,在 [] 之外则代表定位在行首的意义!






    找出空白行:
    [root@www ~]# grep -n '^$' regular_express.txt22:因为只有行首跟行尾 (^$),所以,这样就可以找出空白行啦!

    任意一个字节 . 与重复字节 *这两个符号在正则表达式的意义如下:
    . (小数点):代表『一定有一个任意字节』的意思;* (星号):代表『重复前一个字符, 0 到无穷多次』的意思,为组合形态假设我需要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我可以这样做:
    [root@www ~]# grep -n 'g..d' regular_express.txt1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The world <Happy> is the same with "glad".因为强调 g 与 d 之间一定要存在两个字节,因此,第 13 行的 god 与第 14 行的 gd 就不会被列出来啦!
     


    如果我想要列出有 oo, ooo, oooo 等等的数据, 也就是说,至少要有两个(含) o 以上,该如何是好?
    因为 * 代表的是『重复 0 个或多个前面的 RE 字符』的意义, 因此,『o*』代表的是:『拥有空字节或一个 o 以上的字节』,因此,『 grep -n 'o*' regular_express.txt 』将会把所有的数据都列印出来终端上!
    当我们需要『至少两个 o 以上的字串』时,就需要 ooo* ,亦即是:[root@www ~]# grep -n 'ooo*' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search keyword.19:goooooogle yes!

    如果我想要字串开头与结尾都是 g,但是两个 g 之间仅能存在至少一个 o ,亦即是 gog, goog, gooog.... 等等,那该如何?[root@www ~]# grep -n 'goo*g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!


    如果我想要找出 g 开头与 g 结尾的行,当中的字符可有可无[root@www ~]# grep -n 'g.*g' regular_express.txt1:"Open Source" is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.


    如果我想要找出『任意数字』的行?因为仅有数字,所以就成为:
    [root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.







    限定连续 RE 字符范围 {}我们可以利用 . 与 RE 字符及 * 来配置 0 个到无限多个重复字节, 那如果我想要限制一个范围区间内的重复字节数呢?
    举例来说,我想要找出两个到五个 o 的连续字串,该如何作?这时候就得要使用到限定范围的字符 {} 了。 但因为 { 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符   来让他失去特殊意义才行。 至於 {} 的语法是这样的,假设我要找到两个 o 的字串,可以是:

    [root@www ~]# grep -n 'o{2}' regular_express.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search ke19:goooooogle yes!
     
    假设我们要找出 g 后面接 2 到 5 个 o ,然后再接一个 g 的字串,他会是这样:
    [root@www ~]# grep -n 'go{2,5}g' regular_express.txt18:google is the best tools for search keyword. 
    如果我想要的是 2 个 o 以上的 goooo....g 呢?除了可以是 gooo*g ,也可以是:
    [root@www ~]# grep -n 'go{2,}g' regular_express.txt18:google is the best tools for search keyword.19:goooooogle yes!



  • 相关阅读:
    Linux shell脚本学习
    正则表达式 学习-编辑
    新装ubuntu使用vi时方向键不起作用
    vi
    ln
    crontab
    64bit ubuntu 14 04 LTS 安装 google chrome
    64bit ubuntu 14 04 LTS 安装 adobe flash player
    glogin.sql的常用配置
    sql基本语句(一)select
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/6599777.html
Copyright © 2011-2022 走看看