shell入门-grep过滤-1
正则表达式,就是一个字符串。有一定的规律。我们用指定的字符串匹配一个指定的行。指定的字符串就是正则表达式。 正则表达式有这几个工具:grep egrep sed awk 命令:gerep 说明:过滤出指定的行 选项:--color 关键字有颜色 -n 显示行号 -c 显示一共出现了多少行 -v 取反 不包含指定字符的行 -A n n指数字 例如A2在有指定字符的行下面再显示两行 -B n n指数字 例如B2 在有指定字符的行上面再显示两行 -C n n指数字 例如C2 在有指定字符的行上面和下面再显示各两行 -r 显示目录里的所以带指定字符的行 -rh 显示目录里的所以带指定字符的行并不显示文件路径和文件名 grep 过滤出有root的行 [root@wangshaojun ~]# grep --color 'root' /etc/passwd root :x:0:0: root :/ root :/bin/bash operator:x:11:0:operator:/ root :/sbin/nologin grep -n [root@wangshaojun ~]# grep -n 'root' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 11:operator:x:11:0:operator:/root:/sbin/nologin