


grep常用用法
[root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename
选项与参数:
-a :将 binary 文件以 text 文件的方式搜寻数据
-c :计算找到 '搜寻字符串' 的次数
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
--color=auto :可以将找到的关键词部分加上颜色的显示喔!
将/etc/passwd,有出现 root 的行取出来
grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号
grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
30:operator:x:11:0:operator:/root:/sbin/nologin
将/etc/passwd,将没有出现 root 的行取出来
grep -v root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
根据文件内容递归查找目录
grep ‘energywise’ * 在当前目录搜索带'energywise'行的文件
grep -r ‘energywise’ * 在当前目录及其子目录下搜索'energywise'行的文件 grep -l -r ‘energywise’ * 在当前目录及其子目录下搜索'energywise'行的文件,但不显示匹配的行,只显示匹配的文件



文章评论 本文章有个评论