zoukankan      html  css  js  c++  java
  • grep

    1.正则表达式


    1.1 基本正则表达式(RE)

     

    匹配释义
    元字符 ^
    行首锚定
    $
    行尾锚定
    .
    任意单个字符
    *
    不代表字符(属于限定符),匹配其前导字符的任意次
    [ ]
    字符集匹配
    [^ ]
    不匹配其中列出的任意字符
    ()
    后续表达式通过“转义序列”(1)来引用
    x{m,n}
    匹配字符x出现的次数区间;表示最少时需要逗号
    <
    词首锚定
    >
    词尾锚定
    组合
    .*
    这个组合表示任意字符的任意长度
    e*
    表示空,全部匹配(因为星号表示任意数量,也包括零)
    ?
    匹配其前字符 1 次或 0 次
    b{1,1}
    匹配字母“b” 1 次
    b{2,3} 匹配字母“b” 最少 2 次,至多 3 次
    b{3} 匹配字母“b”3次
    b{,3} 错误
    b{3,} 匹配字母“b” 最少 3 次
    b{0,3}a
    i_f02.gif

     

    匹配行首开始的年份“1983-02”、“1983-02-06_17:33:26”:

    $ vlinkstatus -a | grep "^[[:digit:]]{4}-([[:digit:]]{2})"
    $ vlinkstatus -a | grep "^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}_[[:digit:]]{2}(:)[[:digit:]]{2}1[[:digit:]]{2}"

    “.”代表一个字符,不能省略。无法匹配下面的“file”。

    [web@h p]$ cat re.txt
    file
    file1
    file22
    file3 rc
    [web@h p]$ grep "file.$" re.txt
    file1
    [web@h p]$ grep "file." re.txt
    file1
    file22
    file3 rc
    [web@h p]$

     

    在文件“re.txt”追加一行“file4 ”,末尾是个空格。

    [web@h p]$ echo "file4 " >> re.txt
    [web@h p]$ grep "file.$" re.txt
    file1
    [web@h p]$ grep "file. $" re.txt
    file4 
    [web@h p]$ grep "file..$" re.txt
    file22
    file4

     

    1.2 扩展正则表达式(ERE)

    元字符释义
    ① - 与基本正则表达式意义、用法完全相同
    +
    匹配其前导字符最少一次(属于限定符)
    ?
    前导字符最多出现一次(属于限定符);零次或者一次
    |
    表示多个表达式之间或的关系
    ( )
    表示一组可选值的集合;通常与竖线一起使用、不仅于此

     

    [web@h p]$ egrep "file[[:digit:]]+" re.txt
    file1
    file22
    file3 rc
    file4 
    [web@h p]$ egrep "file[[:digit:]]?" re.txt
    file
    file1
    file22
    file3 rc
    file4 
    [web@h p]$ egrep "e(1|3)" re.txt
    file1
    file3 rc
    [web@h p]$

    1.3 Perl正则表达式

     

    元字符释义
    d

    匹配从0到9中的任意一个数字字符;“[0-9]”

    D

    匹配一个非数字字符;“[^0-9]”

    s 匹配任意空白字符 ;空格、制表……,“[f v]”
    S 匹配任意非空白字符

     

    [web@h p]$ grep -P "ed" re.txt
    file1
    file22
    file3 rc
    file4 
    [web@h p]$ grep -P "eds" re.txt
    file3 rc
    file4

     

    1.4 空白字符

    任意空白字符 ”,命令行体验一下;包括“f v”。

     

    表达式意义翻译
    f form feed 换页
    new line 换行

    carriage return 回车
    horizontal tab 水平制表
    v vertical tab 垂直制表

     

    [web@h p]$ echo -e "hellofworld"
    world             # 这个命令执行后,就清屏了,只剩下个“world”了。
    [web@h p]$ echo -e "hello
    world"
    hello
    world
    [web@h p]$ echo -e "hello
    world"
    world
    [web@h p]$ echo -e "hello	world"
    hello    world
    [web@h p]$ echo -e "hellovworld"
    hello
         world
    [web@h p]$ echo -e "hellovvworld"
    hello
    
         world

    1.5 字符集

    字符类释义
    [:alpha:] 匹配一个字母;“[a-zA-Z]”
    [:digit:] 匹配一个数字;“[0-9]”
    [:alnum:] 匹配一个字母或数字;“[0-9a-zA-Z]”
    [:lower:] 小写字母
    [:upper:] 大写字母
    [:space:] 一个空白字符;空格、制表符、换行……
    [:blank:] 空格、制表符
    [:graph:] 一个看得见的可打印字符;不包括空白字符
    [:print:] 大于上边,包括空白字符;不包括控制字符、字符串结束符“”、EOF文件结束符(-1)
    [:cntrl:] 匹配一个控制字符(ASCII字符集中的前32个字符)
    [:punct:] 匹配一个标点符号
    [:xdigit:] 匹配十六进制数字

     

    2.grep


     

    基本语法
    grep [option] pattern [file]...
    选项说明
    输出控制

    -c

    只打印匹配的行数
    --color 高亮显示匹配到的内容
    -o 只显示被匹配到的字符串
    -h 处理多个文件时,不显示文件名
    -l 只显示匹配到的文件名称(跟上边相反)
    -n

    显示匹配到的行,并显示行号

    -q 不输出匹配结果;而依据程序退出状态判断匹配与否
    匹配控制
    -i 忽略大小写匹配
    -v 显示不匹配的文件行
    -w
    匹配整个单词
    -x
    匹配整个文本行
    -F 不支持正则表达式
    -E
    支持扩展正则表达式
    -P
    支持perl正则表达式
    上下行控制

    -A "n"

    匹配行往下显示n行
    -B "n"
    匹配行往上显示n行
    -C "n"
    匹配行往上、下显示n行
    文件和目录选择
    -r 递归搜索所有的目录下的文件

     

    直接打印出符合条件的行数,而不用另外调用“wc -l”:

    $ grep docBase server.xml -c
    1

    1. eg.1.全文搜索:
      #!/bin/bash
      
      #tree -if $1 | grep ".php$"
      echo -e '     path:	' $1
      echo -e '   string:	' $2
      echo -e 'extension:	' $3
      echo ""
      
      for i in $(tree -if $1 | grep ".${3}$")
      do
          if [ -f $i ]
          then
              #echo "hello " $i
              #grep --color -l $2 $i && echo "filename: " $i
              grep --color -l $2 $i
              grep --color $2 $i
              #echo grep -q $2 $i && echo -n "match: "$(grep -c $2 $i) && echo -e "	filename: " $i
          fi
      done
      View Code

    grep.

    一切代码都是为了生活,一切生活都是调剂
  • 相关阅读:
    有一天人人都会变成程序猿
    mysql 假设存在id则设数据自添加1 ,不存在则加入。java月份计算比較
    做程序员的老婆应该注意的一些事情
    人类科技的发展为什么会是加速度的(TRIZ方法再推荐)
    Unity5.0 RPG角色扮演历险类游戏之 森林历险记
    linux目录对照命令——meld
    iOS --- [持续更新中] iOS移动开发中的优质资源
    【spring bean】spring中bean的懒加载和depends-on属性设置
    【spring bean】 spring中bean之间的引用以及内部bean
    【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean
  • 原文地址:https://www.cnblogs.com/argor/p/7909241.html
Copyright © 2011-2022 走看看