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的各种用法的更多相关文章

  1. perl之grep函数的用法

    转载至 perl中grep的详细用法 grep有2种表达方式: 1 grep BLOCK LIST 2 grep EXPR, LIST BLOCK表示一个code块,通常用{}表示:EXPR表示一个表 ...

  2. linux中grep命令的用法

    作为linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很有必要的. 首先谈一下grep命令的常用格式为:[grep  [选项]  "模式"  [ ...

  3. 查找库中的某个函数,grep命令的用法。

    程序中调用了某个库中的函数,我想知道这个函数具体的作用,就必须去看这个库的源代码. 那么问题来了:如何从库中众多的.h文件中,得知我想要的函数在哪个文件里? 最后用grep命令成功解决. 具体用法:先 ...

  4. Linux中find、grep命令详细用法

    在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: find命令 find命令的一般形式 ...

  5. git grep的一些用法

    https://www.kernel.org/pub/software/scm/git/docs/git-grep.html   把所有本地分支包含某个字符的行列出来,把含有master的列出来 gi ...

  6. grep命令相关用法

    grep命令相关参数: -i:忽略大小写 --color:高亮显示匹配到的信息 -v:反向查找,没匹配到的行显示出来 -o:只显示被模式匹配到的串本身 正则表达式: .*:任意长度的任意字符,贪婪模式 ...

  7. Linux find、grep命令详细用法

    在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下:find命令 find命令的一般形式 ...

  8. grep的若干用法

    查找包含server或者client的行 egrep 'server|client' file-name /usr/xpg4/bin/grep -E 'server|client' file-name ...

  9. shell grep的基本用法

    grep 1.-i 不区分大写小写 2.-n 区分大小写 3.-E 查找多个条件 4.-c 查找到的结果的行数 5.-w 精确查找 6.取反 7.-q 静默输出

随机推荐

  1. PHP扩展开发-测验成功

    原文:http://kimi.it/496.html http://blog.csdn.net/u011957758/article/details/72234075 ---------------- ...

  2. C语言开发函数库时利用不透明指针对外隐藏结构体细节

    1 模块化设计要求库接口隐藏实现细节 作为一个函数库来说,尽力降低和其调用方的耦合.是最主要的设计标准. C语言,作为经典"程序=数据结构+算法"的践行者,在实现函数库的时候,必定 ...

  3. root用户不能改动iptables

    需求:因为防火墙做了IP和port限制,如今须要加入一条规则使之能够訪问指定的IP和port. vi /etc/sysconfig/iptables, 加入完毕后,wq保存,提示文件仅仅读无法保存.! ...

  4. HDU 5291(Candy Distribution-差值dp)

    Candy Distribution Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. CentOS 7通过yum安装fcitx五笔输入法

    CentOS 7通过yum安装fcitx五笔输入法 下面通过了亲測: 1.设置源 Posted in Linux at 三月 5th, 2015 / No Comments ? 增加EPEL源 EPE ...

  6. tslib-触摸屏校准

    5.1移植tslib 5.1.1在https://github.com/kergoth/tslib下载最新的tslib 5.1.2为虚拟机里的Linux系统安装工具  sudo apt-get ins ...

  7. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

    E. Correct Bracket Sequence Editor   Recently Polycarp started to develop a text editor that works o ...

  8. HDU3496 Watch the Movie 背包

    题目大意:给你n张电影门票,但一次只可以买m张,并且你最多可以看L分钟,接下来是n场电影,每一场电影a分钟,b价值,要求恰好看m场电影所得到的最大价值,要是看不到m场电影,输出0. 三个限制: 选电影 ...

  9. Php learn note

    Php learn note 1. Between two part of ECHO, there is , sign rather than + sign. echo 'Hello World!!' ...

  10. nyoj--170--网络的可靠性(水题)

    网络的可靠性 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 A公司是全球依靠的互联网解决方案提供商,也是2010年世博会的高级赞助商.它将提供先进的网络协作技术,展示其 ...