1、在当前目录中,查找后缀有 file 字样的文件中包含 test 字符串的文件,并打印出该字符串的行.
此时,可以使用如下命令:
1
grep test *file
结果如下所示:
1 2 3 4
$ grep test test* #查找前缀有"test"的文件包含"test"字符串的文件 testfile1:This a Linux testfile! #列出testfile1 文件中包含test字符的行 testfile_2:This is a linux testfile! #列出testfile_2 文件中包含test字符的行 testfile_2:Linux test #列出testfile_2 文件中包含test字符的行
$ grep -r update /etc/acpi #以递归的方式查找"etc/acpi" #下包含"update"的文件 /etc/acpi/ac.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.) Rather than /etc/acpi/resume.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.) Rather than /etc/acpi/events/thinkpad-cmos:action=/usr/sbin/thinkpad-keys--update
3、反向查找.
前面各个例子是查找并打印出符合条件的行,通过”-v”参数可以打印出不符合条件行的内容.
查找文件名中包含 test 的文件中不包含test 的行,此时,使用的命令为:
1
grep -v test *test*
结果如下所示:
1 2 3 4 5 6 7 8 9
$ grep-v test* #查找文件名中包含test 的文件中不包含test 的行 testfile1:helLinux! testfile1:Linis a free Unix-type operating system. testfile1:Lin testfile_1:HELLO LINUX! testfile_1:LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM. testfile_1:THIS IS A LINUX TESTFILE! testfile_2:HELLO LINUX! testfile_2:Linux is a free unix-type opterating system.