不知不觉已经周四了,感觉过了周三就是星期天啊,又要休息了,想想真是开心,今天给大加说一下grep命令,所有的类raksmart服务器Linux系统都会提供一个名为grep(global regular expression print,全局正则表达式输出)的搜索工具。grep命令在对一个或多个文件的内容进行基于模式的搜索的情况下是非常有用的。模式可以是单个字符、多个字符、单个单词、或者是一个句子。当命令匹配到执行命令时指定的模式时,grep会将包含模式的一行输出,但是并不对原文件内容进行修改,接下来说5个grep命令的例子吧。

1 在文件中查找模式(单词)

在/etc/passwd文件中查找单词“linuxtechi”

root@Linux-world:~# grep linuxtechi /etc/passwd

linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash

root@Linux-world:~#

2 在多个文件中查找模式。

root@Linux-world:~# grep linuxtechi /etc/passwd /etc/shadow /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:!::linuxtechi

root@Linux-world:~#

3 使用-l参数列出包含指定模式的文件的文件名。

root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab

/etc/passwd

/etc/shadow

root@Linux-world:~#

4 使用-n参数,在文件中查找指定模式并显示匹配行的行号

root@Linux-world:~# grep -n linuxtechi /etc/passwd

39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash

root@Linux-world:~#

root@Linux-world:~# grep -n root /etc/passwd /etc/shadow

5个使用grep命令的例子

5 使用-v参数输出不包含指定模式的行

输出/etc/passwd文件中所有不含单词“linuxtechi”的行

root@Linux-world:~# grep -v linuxtechi /etc/passwd

5个使用grep命令的例子

好了以上就是使用grep的5个例子,当然不止这五个哈,但是多写几个就太长了,小编太懒凑活看看得了,咱们明天见!