grep的各种用法
1. 在文件中查找模式(单词)
在/etc/passwd文件中查找单词 root
[root@localhost opt]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2. 在多个文件中查找模式
在/etc/passwd /etc/shadow /etc/gshadow文件中查找单词 root
[root@localhost opt]# grep root /etc/{passwd,shadow,gshadow}
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:root:$1$NVmZi4Ax$SF9f.N2XrWRlIhj4T7F5F/::0:99999:7:::
/etc/gshadow:root:::
3. 使用-l参数列出包含指定模式的文件的文件名
在/etc/passwd /etc/shadow /etc/gshadow文件中查找单词 root
[root@localhost opt]# grep -l root /etc/{passwd,shadow,gshadow}
/etc/passwd
/etc/shadow
/etc/gshadow
4. 使用-n参数,在文件中查找指定模式并显示匹配行的行号
grep -n root /etc/{passwd,shadow,gshadow}
/etc/passwd:1:root:x:0:0:root:/root:/bin/bash
/etc/passwd:10:operator:x:11:0:operator:/root:/sbin/nologin
/etc/shadow:1:root:$1$NVmZi4Ax$SF9f.N2XrWRlIhj4T7F5F/::0:99999:7:::
/etc/gshadow:1:root:::
5. 使用-v参数输出不包含指定模式的行
[root@localhost opt]# grep -v root /etc/{passwd,shadow,gshadow}
/etc/passwd:bin:x:1:1:bin:/bin:/sbin/nologin
/etc/passwd:daemon:x:2:2:daemon:/sbin:/sbin/nologin
/etc/passwd:adm:x:3:4:adm:/var/adm:/sbin/nologin
/etc/passwd:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
/etc/passwd:sync:x:5:0:sync:/sbin:/bin/sync
...........................
/etc/passwd:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
/etc/passwd:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
/etc/passwd:mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
/etc/passwd:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
/etc/passwd:cactiuser:x:1000:1000::/home/cactiuser:/bin/bash
/etc/passwd:ntp:x:38:38::/etc/ntp:/sbin/nologin
/etc/shadow:bin:*:17110:0:99999:7:::
/etc/shadow:daemon:*:17110:0:99999:7:::
/etc/shadow:adm:*:17110:0:99999:7:::
...........................
/etc/shadow:chrony:!!:17529::::::
/etc/shadow:sshd:!!:17529::::::
/etc/shadow:mysql:!!:17569::::::
/etc/shadow:apache:!!:17569::::::
/etc/shadow:cactiuser:!!:17569:0:99999:7:::
/etc/shadow:ntp:!!:17569::::::
/etc/gshadow:bin:::
/etc/gshadow:daemon:::
/etc/gshadow:sys:::
...........................
6. 使用 ^ 符号输出所有以某指定模式开头的行
[root@localhost opt]# grep ^root /etc/{passwd,shadow,gshadow}
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/shadow:root:$1$NVmZi4Ax$SF9f.N2XrWRlIhj4T7F5F/::0:99999:7:::
/etc/gshadow:root:::
7. 使用 $ 符号输出所有以指定模式结尾的行
[root@localhost opt]# grep bash$ /etc/{passwd,shadow,gshadow}
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:cactiuser:x:1000:1000::/home/cactiuser:/bin/bash
8. 使用 -R (或者-r )参数递归地查找特定模式 (这个模式只能接目录)
[root@localhost opt]# grep -R bash /etc/
/etc/rc.local:#!/bin/bash
/etc/shells:/bin/bash
/etc/shells:/usr/bin/bash
/etc/passwd-:root:x:0:0:root:/root:/bin/bash
/etc/passwd-:cactiuser:x:1000:1000::/home/cactiuser:/bin/bash
/etc/bash_completion.d/git:# bash/zsh completion support for core Git.
/etc/bash_completion.d/git:# 2) Add the following line to your .bashrc/.zshrc:
...........................
9. 使用 Grep 查找文件中所有的空行
[root@localhost ~]# grep ^$ /etc/passwd
[root@localhost ~]#
10 .使用 -I 参数查找模式
grep命令的-i参数在查找时忽略字符的大小写。
[root@localhost ~]# grep -i "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
11. 使用 -e 参数查找多个模式
在一条grep命令中查找‘bash’和‘root’单词,使用-e参数,
[root@localhost ~]# grep -e {"root","bash"} /etc/passwd
grep: bash: No such file or directory
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# grep -e "root" -e "bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
cactiuser:x:1000:1000::/home/cactiuser:/bin/bash
12. 使用 -f 用文件指定待查找的模式
[root@localhost ~]# cat>>pp<<eof
bash
root
echo
eof
[root@localhost ~]# grep -f pp /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologi
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
13.使用 -c 参数计算模式匹配到的数量
[root@localhost ~]# grep -f pp -c /etc/passwd
20
14. 输出匹配指定模式行的前或者后面N行
使用-B参数输出匹配行的前2行
[root@localhost ~]# grep -B 2 "games" /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
使用-A参数输出匹配行的后2行
[root@localhost ~]# grep -A 2 "games" /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
使用-C参数输出匹配行的前后2行
[root@localhost ~]# grep -C 2 "games" /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
grep的各种用法的更多相关文章
- perl之grep函数的用法
转载至 perl中grep的详细用法 grep有2种表达方式: 1 grep BLOCK LIST 2 grep EXPR, LIST BLOCK表示一个code块,通常用{}表示:EXPR表示一个表 ...
- linux中grep命令的用法
作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很有必要的. 首先谈一下grep命令的常用格式为:[grep [选项] "模式" [ ...
- 查找库中的某个函数,grep命令的用法。
程序中调用了某个库中的函数,我想知道这个函数具体的作用,就必须去看这个库的源代码. 那么问题来了:如何从库中众多的.h文件中,得知我想要的函数在哪个文件里? 最后用grep命令成功解决. 具体用法:先 ...
- Linux中find、grep命令详细用法
在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...
- git grep的一些用法
https://www.kernel.org/pub/software/scm/git/docs/git-grep.html 把所有本地分支包含某个字符的行列出来,把含有master的列出来 gi ...
- grep命令相关用法
grep命令相关参数: -i:忽略大小写 --color:高亮显示匹配到的信息 -v:反向查找,没匹配到的行显示出来 -o:只显示被模式匹配到的串本身 正则表达式: .*:任意长度的任意字符,贪婪模式 ...
- Linux find、grep命令详细用法
在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下:find命令 find命令的一般形式 ...
- grep的若干用法
查找包含server或者client的行 egrep 'server|client' file-name /usr/xpg4/bin/grep -E 'server|client' file-name ...
- shell grep的基本用法
grep 1.-i 不区分大写小写 2.-n 区分大小写 3.-E 查找多个条件 4.-c 查找到的结果的行数 5.-w 精确查找 6.取反 7.-q 静默输出
随机推荐
- Linux显示使用命令who(转)
Linux who命令用于显示系统中有哪些使用者正在上面,显示的资料包含了使用者ID.使用的终端机.从哪边连上来的.上线时间.呆滞时间.CPU使用量.动作等等. 使用权限:所有使用者都可使用. 语法 ...
- mysql稳定的版本号选择及下载说明(2014-11-10)
怎样选择新稳定的版本号 mysql的版本号大概能够分为Alpha.Beta.GA. GA版即mysql官方公布的稳定版本号. 怎样在官方下载Mysql 能够通过http:// ...
- linux 文件操作系统调用
crate:创建文件 open:打开文件 read:读取文件 write:写文件 lseek :设置文件偏移量 access:推断文件 close:关闭文件的读写操作
- python TypeError: 'builtin_function_or_method' object is not iterable keys
statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statin ...
- 新建maven web工程报错
问题: 检查本地仓库: 检查1.0跟release的文件夹: 试试:http://www.ithao123.cn/content-8028507.html 然后选择maven catalog下的:(这 ...
- C语言中为什么要使用enum
转载请注明出处,否则将追究法律责任http://blog.csdn.net/xingjiarong/article/details/47275971 在C语言中有一个关键字是enum,枚举类型,不知道 ...
- Oracle 数据块损坏与恢复具体解释
1.什么是块损坏: 所谓损坏的数据块,是指块没有採用可识别的 Oracle 格式,或者其内容在内部不一致. 通常情况下,损坏是由硬件故障或操作系统问题引起的.Oracle 数据库将损坏的块标识为&qu ...
- hdoj--1150--Machine Schedule(最小点覆盖)
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- php对文件/目录操作的基础知识(图解)
具体的如下图所示:
- VirtualBox里如何正确安装增强工具(图文详解)
不多说,直接上干货! 找到 复制到