• Linux学习之正则表达式grep


    1. 关键词显示颜色,且加上行号

      dmesg|grep -n --color=auto 'eth'

    2. 将关键词所在行的前2行和后3行,也一起显示

      dmesg|grep -n -A3 -B2 --color=auto 'eth'

    3. 自动将关键词加上颜色

      vim ~/.bashrc

      alias grep='grep --color=auto'

      source ~/.bashrc

    4. 在某个档案中搜索特定字符串

      gerp -n 'the' regular_express.txt

    5. 反向选择,搜索不含特定字符串的行

      grep -vn 'the' regular_express.txt

    6. 不区分大小写搜索特定字符串

    7. grep -

    in 'the' regular_express.txt

    7. 查找test或taste

      grep -n 't[ea]st' regular_express.txt

    8. 不包含某字符

      grep -n '[^g]oo' regular_express.txt

    9. 搜索数字所在行

      grep -n '[0-9]' regular_express.txt

     grep -n '[[:digit:]]' regular_express.txt

    10. ^在[]内代表非,在[]外代表行首

      gerp -n '^the' regular_express.txt(搜索行首是the的行)

     grep -n '^[[:lower:]]' regular_express.txt(搜索行首是小写字母的行)

    11.行尾以小数点.结尾的

     grep -n '.$' regular_express.txt

    12.不要文档中空白行和开头带#的行

     grep -v '^$' /etc/syslog.conf | grep -v '^#'

    13.一定有一个任意字符:.(小数点)

     grep -n 'g..d' regular_express.txt

    14.重复0个或多个前面的字符:*(星号)

     grep -n 'ooo*' regular_express.txt(至少存在2个oo)

    15.找出g开头和g结尾的字符串,当中的字符可有可无

     grep -n 'g.*g' regular_express.txt

    16.限定连续字符范围:{}

    grep -n 'go{2,5}g' regular_express.txt(找出g后面接2到5个o的)

    17.列出etc下的链接档案数量:ll /etc|grep '^l'|wc -l

  • 相关阅读:
    二分图匹配初步
    动态规划初步
    一些排日程的经典方法
    petri网初步
    笔记:美国院士教你写论文
    Ubuntu18.04彻底删除MySQL数据库
    ubuntu18.04 安装 wps2019
    ubuntu18.04 阿里镜像源
    Ubuntu 18.04 使用标准Ubuntu 仓库进行自动化安装NVIDIA驱动
    linux maven环境变量配置
  • 原文地址:https://www.cnblogs.com/enginex/p/6802708.html
Copyright © 2020-2023  润新知