zoukankan      html  css  js  c++  java
  • Linux中的grep 命令

     介绍grep文本处理命令,它也可以解释正则。

    常用选项:
      -E :开启扩展(Extend)的正则表达式。
      -i :忽略大小写(ignore case)。
      -v :反过来(invert),只打印没有匹配的,而匹配的反而不打印。
      -n :显示行号
      -w :被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker
         -c :显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项是显示有多少行没有被匹配到。
      -o :只显示被模式匹配到的字符串。
      --color :将匹配到的内容以颜色高亮显示。
      -A  n:显示匹配到的字符串所在的行及其后n行,after
      -B  n:显示匹配到的字符串所在的行及其前n行,before
      -C  n:显示匹配到的字符串所在的行及其前后各n行,context
           -q : 静默模式,没有任何输出,得用$?来判断执行成功没有,即有没有过滤到想要的内容
           -w :匹配单词,单词的意思是特殊符号隔开的英文字母组合,对象中的正则表达式都会当成单词(前后有特殊符号)。
       
    grep种类
    grep   全面搜索正则表达式并把行打印出来
    fgrep   它搜索字符串而不是搜索匹配表达式的模式。fgrep 命令使用快速的压缩算法。$, *, [, |, (, )和等字符串被 fgrep 命令按字面意思解释。这些字符并不解释为正则表达式
    pgrep   以名称为依据从运行进程队列中查找进程,并显示查找到的进程id。
    egrep   用于在文件内查找指定的字符串。egrep执行效果与grep -E相似,使用的语法及参数可参照grep指令,与grep的不同点在于解读字符串的方法。

    正则介绍

     1.定义变量边界

    [root@master ~]# test=30
    [root@master ~]# echo ${test}%
    30%

    2.运算符
    算术运算符:+、-、*、/、%

    [root@master ~]# echo $[3+3]
    6

    3.关系操作:与(())连用
    <    >     <=     >=     ==     !=     &&     ||

    [root@master ~]# x=2
    [root@master ~]# [ $x -gt 1 ]
    [root@master ~]# echo $?
    0

    4.shell中的计算器

    [root@master ~]# echo 'scale=2;1/3'|bc -l
    .33
    [root@master ~]# echo 'scale=3;1/3'|bc -l
    .333

    5.查找一个文件中空行

    [root@master ~]# more test 
    hello world
    
    the line 3
    
    two
    
    [root@master ~]# grep -c "^$" test
    2

    6.查找在“hello”有任意长度字串的行

    [root@master ~]# grep ".*hello" test
    hello world

    7.查找含有特殊字符

    [root@master ~]# grep "192.168.1.1" test
    192.168.1.1 ip

    8.不显示本身进程

    [root@master ~]# ps -ef|grep /usr/lib/systemd/systemd |grep -v "grep"
    root          1      0  0 14:34 ?        00:00:04 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
    root        604      1  0 14:34 ?        00:00:00 /usr/lib/systemd/systemd-journald
    root        647      1  0 14:34 ?        00:00:01 /usr/lib/systemd/systemd-udevd
    root        854      1  0 14:34 ?        00:00:00 /usr/lib/systemd/systemd-logind

    9.查看以字符h开头的

    [root@master ~]# cat test|grep ^h
    hello world

    10输出ip

    [root@master ~]# ifconfig ens33 |grep -E "([0-9]{1,3}.){3}[0-9]"
            inet 192.168.1.200  netmask 255.255.255.0  broadcast 192.168.1.255

    11.输出包含he和ld 的行

    [root@master ~]# cat test|grep -E "he|ld"
    hello world
    the line 3

    12.查看文件中小写字母连续有5个以上的字符

    [root@master ~]# grep '[a-z]{5}' test
    hello world
    asdfghjjkl

    13.匹配到的字符用颜色显示出来

    [root@master ~]# grep --color "hello" test
    hello world
  • 相关阅读:
    VSCode 配置 Python 开发环境
    出现:Microsoft Visual C++ 14.0 is required 的解决方案
    python3 pathlib库中的Path类的使用
    使用 AI 绘制箭头
    Adobe Illustrator 入门 新建 保存图片
    jinja2
    Java 读取和写入文本文件
    Affy包 estrogen包
    GEOquery
    apply() 函数家族介绍
  • 原文地址:https://www.cnblogs.com/hello-wei/p/10137386.html
Copyright © 2011-2022 走看看