grep及正则表达式系列---01

grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包括grep、egrepfgrep正则表达式

   grep:根据模式搜索文本,并将符合模式的文本行显示出来。
bash

   Pattern:由文本字符和正则表达式的元字符组合而成的匹配条件。
ide

   grep [OPTIONS] PATTERN [FILE...]工具

       例子:# grep 'root' /etc/passwdspa

           root:x:0:0:root:/root:/bin/bashhtm

           operator:x:11:0:operator:/root:/sbin/nologin字符串

   grep经常使用相关选项:
get

       -i:忽略大小写    it

           例子:# grep -i 'root' /etc/passwdio

               root:x:0:0:root:/root:/bin/bash

               operator:x:11:0:operator:/root:/sbin/nologin


       --color:带颜色显示


           例子:# grep --color 'root' /etc/passwd

root:x:0:0:root:/root:/bin/bash

               operator:x:11:0:operator:/root:/sbin/nologin


注意:也能够给grep取别名带颜色显示。

           例子:# aliasgrep='grep --color '

                 # grep 'root' /etc/passwd

root:x:0:0:root:/root:/bin/bash

              operator:x:11:0:operator:/root:/sbin/nologin


       -v:反向查找,显示没有被模式匹配到的行。

           例子:# grep -v 'root' /etc/passwd

               bin:x:1:1:bin:/bin:/sbin/nologin

               daemon:x:2:2:daemon:/sbin:/sbin/nologin

               adm:x:3:4:adm:/var/adm:/sbin/nologin

               ......

       -o:只显示被模式匹配到的字符串。

           例子:# grep -o 'root' /etc/passwd

               root

               root

               root

               root

相关文章
相关标签/搜索