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 静默输出
随机推荐
- 状态压缩dp poj 3254 hdu5045
近来感觉状态压缩dp的强大性(灵活利用了二进制运算非常关键). . . 于是做了俩提来看看..毕竟队友是专业的dp.我仅仅是管中窥豹下而已.. 日后有机会再与之玩耍玩耍...ps:假设上天再给我一次机 ...
- Android-68-Tomcat各种启动错误的解决的方法,如:Exception in thread "Thread-6" NoClassDefFoundError,Document base E:\
上午遇到一个棘手的事儿,导入一个project,结果把原有的Tomcatserver给导坏了.各种红的.黑的.蓝的错误满天飞啊,刚弄完一个项目,怕被毁了.我那个揪心呀! 还好.在走头无路的情况下 ...
- zoj3886--Nico Number(素数筛+线段树)
Nico Number Time Limit: 2 Seconds Memory Limit: 262144 KB Kousaka Honoka and Minami Kotori are ...
- Kafka集群部署及測试
题记 眼下我们对大数据进行研究方向以Spark为主,当中Spark Streaming是能够接收动态数据流并进行处理.那么Spark Streaming支持多源的数据发送端,比如TCP.ZeroMQ. ...
- Jetty:部署到Jetty
Web应用的框架 标准Jetty公布版本号能部署标准servlet Spec Web应用和Jetty内部ContextHandler部署描写叙述符,或者两者的一个混合. Web应用是可部署的动态(se ...
- POJ3255 Roadblocks 严格次短路
题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...
- oc7--内存分析
// // main.m // 第二个OC类 #import <Foundation/Foundation.h> @interface Person : NSObject { @publi ...
- [转载]H3C S3600 DHCP-SERVER 配置【原创】
原文地址:H3C S3600 DHCP-SERVER 配置[原创]作者:旅行者萧 案例要求: 在H3C S3600-28TP-SI 的vlan 里配置DHCP server,使用vlan 里部分网段为 ...
- C - Game With Sticks
Problem description After winning gold and silver in IOI 2014, Akshat and Malvika want to have some ...
- First step in troubleshooting complex issues: Define and scope your issue properly
最近在查调试相关资料的时候,无意看到Tess的一篇关于如何快速分析复合场景问题的博文,感觉很实用,Mark备忘. My 9 questions for a pretty thorough proble ...