zoukankan      html  css  js  c++  java
  • Linux grep命令和正则表达式

    介绍

    grep是一个功能强大的文本搜索命令,可以用它来搜索某个文件中是否包含指定的搜索内容,它可以利用正则表达式来做复杂的筛选操作,它还可以为其它命令传输给管道的筛选,比如我们常用到的分析单个进程的操作就是会利用它“ps -ef|grep command”。

    语法

    grep [OPTION]... PATTERN [FILE]...

    默认不加参数是显示匹配上的行记录,可以使用--help来查看它所支持的所以参数,本文只会列举比较常用的一些命令。

    -a:输出的内容不要忽略二进制数据
    -b<n>:输出匹配上的n个字节的行。
    -c :只显示符合条件的行的数量,不显示内容
    -d:当你要查找的是目录而不是文件的时候需要制定该参数,否则会报错
    -H:在输出的内容行前加上该行所属的文件名。
    -h:不在输出的行前加上该行所属的文件名,这是默认的选项。
    -i:忽略大小写
    -L:列出不符合查找内容的文件的文件名
    -l:列出符合查找内容的文件的文件名
    -m<n>:只输出匹配上的指定n行。
    -o:只显示查找的内容,不显示该行其它的内容。
    -q:什么都不输出
    -r:如果需要遍历整个目录的所有文件,可以使用该参数
    -v:显示没有匹配上的行信息,和默认值相反
    -V:显示版本信息

    正则表达式

    grep配合正则进行筛选的时候对于{ } ()都需要用到转义字符。

    命令

    说明

    ^

    在字符的开启处进行匹配

    $

    在字符的末尾处进行匹配

    .

    匹配任何字符(包括回车和新行)

    [….]

    匹配括号内的任意单个字符

    [m-n]

    匹配m到n之间的任意单个字符,例如[0-9],[a-z],[A-Z]

    [^..]

    不能匹配括号内的任意单个字符

    a*

    匹配0个或多个a,包括空

    a{m}

    匹配m个a

    a{m,}

    匹配m个或者更多个a

    a{m,n}

    匹配m到n个a

    (….)

    将模式元素组成单一元素,例如(do)*意思是匹配0个多或多个do

    grep常见用法

    创建测试数据

    grep --help >/tmp/grep.text

    1.为其它命令做筛选操作

    查询包含sbin的进程

    [root@localhost ~]# ps -ef |grep "sbin"
    root 1 0 0 11:04 ? 00:00:01 /sbin/init
    root 543 1 0 11:04 ? 00:00:00 /sbin/udevd -d
    root 1559 1 0 11:04 ? 00:00:00 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other /var/run/vmblock-fuse
    root 1580 1 0 11:04 ? 00:00:18 /usr/sbin/vmtoolsd
    root 1992 1 0 11:04 ? 00:00:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
    root 2078 1 0 11:04 ? 00:00:00 /usr/sbin/modem-manager
    root 2122 1 0 11:04 ? 00:00:00 /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -B -u -f /var/log/wpa_supplicant.log -P /var/run/wpa_supplicant.pid
    root 2133 1 0 11:05 ? 00:00:00 /usr/sbin/acpid
    root 2219 1 0 11:05 ? 00:00:00 /usr/sbin/bluetoothd --udev
    root 2298 1 0 11:05 ? 00:00:00 /usr/sbin/sshd
    root 3172 1 0 11:05 ? 00:00:00 /usr/sbin/abrtd
    root 3199 1 0 11:05 ? 00:00:00 /usr/sbin/atd
    root 3215 1 0 11:05 ? 00:00:00 /usr/sbin/gdm-binary -nodaemon
    root 3220 1 0 11:05 tty2 00:00:00 /sbin/mingetty /dev/tty2
    root 3222 1 0 11:05 tty3 00:00:00 /sbin/mingetty /dev/tty3
    root 3224 1 0 11:05 tty4 00:00:00 /sbin/mingetty /dev/tty4
    root 3227 543 0 11:05 ? 00:00:00 /sbin/udevd -d
    root 3228 1 0 11:05 tty5 00:00:00 /sbin/mingetty /dev/tty5
    root 3230 1 0 11:05 tty6 00:00:00 /sbin/mingetty /dev/tty6
    root 3231 543 0 11:05 ? 00:00:00 /sbin/udevd -d
    root 3261 1 0 11:05 ? 00:00:00 /usr/sbin/console-kit-daemon --no-daemon
    root 5923 2071 0 14:12 ? 00:00:00 /sbin/dhclient -d -4 -sf /usr/libexec/nm-dhcp-client.action -pf /var/run/dhclient-eth0.pid -lf /var/lib/dhclient/dhclient-3a7ff4d9-5a09-46b1-bb20-0298a18e6b78-eth0.lease -cf /var/run/nm-dhclient-eth0.conf eth0
    root 6294 6044 0 15:50 pts/1 00:00:00 grep sbin

    2. 查询行数

    查询包含“-d”的行数
    [root@localhost ~]# grep -c "-d" /tmp/grep.txt 
    6

    3.$

    查询以lines结尾的行

    [root@localhost ~]# grep  "lines$" /tmp/grep.txt 
      -x, --line-regexp         force PATTERN to match only whole lines
      -v, --invert-match        select non-matching lines
      -b, --byte-offset         print the byte offset with output lines
      -n, --line-number         print line number with output lines

    4.{m,}

    查询包含2个或者更多个S的行
    [root@localhost ~]# grep  "(s){2,}" /tmp/grep.txt 
    PATTERN is, by default, a basic regular expression (BRE).
      -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
      -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
      -P, --perl-regexp         PATTERN is a Perl regular expression
      -s, --no-messages         suppress error messages
      -h, --no-filename         suppress the file name prefix on output
      -q, --quiet, --silent     suppress all normal output
          --binary-files=TYPE   assume that binary files are TYPE;
    -r is given, - otherwise.  If fewer than two FILEs are given, assume -h.

    总结

    注意使用转义字符,如果使用正则要查找的不是单个字符而是多个字符需要使用()把多个字符括起来,grep还有很多的使用技巧这里就不一一列出来。

    备注:

        作者:pursuer.chen

        博客:http://www.cnblogs.com/chenmh

    本站点所有随笔都是原创,欢迎大家转载;但转载时必须注明文章来源,且在文章开头明显处给明链接。

    《欢迎交流讨论》

  • 相关阅读:
    NET网络配置
    NTP时间同步器
    NFS配置linux
    DNS配置域名解析
    ftp配置windows
    samba配置与windows
    NTP服务和DNS服务
    NFS服务和DHCP服务scp
    SAMBA服务和FTP服务
    find用法
  • 原文地址:https://www.cnblogs.com/chenmh/p/5342456.html
Copyright © 2011-2022 走看看