Example1Findapattern(word)inafilefindtheword"linuxtechi"inthe/etc/passwdfileroot@Linux-world:~#greplinuxtechi/etc/passwdlinuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#Example2在多个文件中查找模式root@Linux-world:~#greplinuxtechi/etc/passwd/etc/影子/etc/gshadow/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash/etc/shadow:linuxtechi:$6$DdgXjxlM$4flz4JRvefvKp0DG6re:16550:0:99999:7::/etc/gshadow:adm:*::syslog,linuxtechi/etc/gshadow:cdrom:*::linuxtechi/etc/gshadow:sudo:*::linuxtechi/etc/gshadow:dip:*::linuxtechi/etc/gshadow:plugdev:*::linuxtechi/etc/gshadow:lpadmin:!::linuxtechi/etc/gshadow:linuxtechi:!::/etc/gshadow:sambashare:!::linuxtechiroot@Linux-world:~#例3使用-L参数列出包含指定模式的文件的文件名root@Linux-world:~#grep-llinuxtechi/etc/passwd/etc/shadow/etc/fstab/etc/mtab/etc/passwd/etc/shadowroot@Linux-world:~#Example4使用-N参数在文件中查找指定模式并显示匹配行的行号root@Linux-world:~#grep-nlinuxtechi/etc/passwd39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#Example5使用-V参数输出不包含指定模式的行输出/etc/passwd文件中包含指定模式的所有行不包含单词“linuxtechi”root@Linux-world:~#grep-vlinuxtechi/etc/passwd示例6使用^符号输出以指定模式开头的所有行Bash脚本将^符号视为特殊字符并用于指定行或词的开头例如输出/etc/passes文件中所有以“root”开头的行root@Linux-world:~#grep^root/etc/passwdroot:x:0:0:root:/root:/bin/bashroot@Linux-world:~#Example7使用$符号输出所有以指定模式结尾的行输出/etc/passwd文件中所有以"bash"结尾的行root@Linux-world:~#grepbash$/etc/passwdroot:x:0:0:root:/root:/bin/bashlinuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#Bash脚本处理美元($)标志为特殊字符,用于指定一行或一个单词的结束例8使用-R参数递归搜索特定模式root@Linux-world:~#grep-rlinuxtechi/etc//etc/subuid:linuxtechi:100000:65536/etc/group:adm:x:4:syslog,linuxtechi/etc/group:cdrom:x:24:linuxtechi/etc/group:sudo:x:27:linuxtechi/etc/group:dip:x:30:linuxtechi/etc/group:plugdev:x:46:linuxtechi/etc/group:lpadmin:x:115:linuxtechi/etc/group:linuxtechi:x:1000:/etc/group:sambashare:x:131:linuxtechi/etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash......................................................…………......上面的命令会在/etc目录下递归查找“linuxtechi”这个词例9使用Grep查找文件root@Linux-world中的所有空行:~#grep^$/etc/shadowroot@Linux-world:~#因为/etc/shadow文件中没有空行,所以没有输出例10使用-I参数查找模式grep命令的-i参数忽略搜索时字符的大小写让我们看一个例子,在密码文件nextstep4it@localhost:~$grep-iLinuxTechi/etc/passwdlinuxtechi:x:1001:1001::/home/linuxtechi:/bin/bashnextstep4it@localhost中寻找单词“LinuxTechi”:~$Example11使用-E参数查找多个模式例如,我想在一个grep命令中查找单词'linuxtechi'和'root',使用-e参数,我们可以找到多个模式root@Linux-world:~#grep-e"linuxtechi"-e"root"/etc/passwdroot:x:0:0:root:/root:/bin/bashlinuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#Example12使用-F指定要用文件搜索的模式。首先,在当前目录中创建一个搜索模式文件“grep_pattern”。我想在文件root@Linux-world中输入以下内容:~#catgrep_pattern^linuxtechirootfalse$root@Linux-world:~#现在,尝试使用grep_pattern文件搜索root@Linux-world:~#grep-fgrep_pattern/etc/passwdExample13使用-C参数计算模式匹配的个数继续上面的例子,我们在grep命令中使用-c命令来统计匹配指定模式的个数或者接下来的N行a)使用-B参数输出匹配行的前4行root@Linux-world:~#grep-B4"games"/etc/passwdb)使用-A参数输出匹配行的后4行root@Linux-world:~#grep-A4"游戏&曲ot;/etc/passwdc)使用-C参数输出匹配行前后4行root@Linux-world:~#grep-C4"games"/etc/passwd
