2、在文件夹 dir 中递归查找所有文件中匹配正则表达式 "pattern"
的行,并打印匹配行所在的文件名和行号:
grep -r -n pattern dir/
3、在标准输入中查找字符串 "world",并只打印匹配的行数:
echo "hello world"| grep -c world
4、在当前目录中,查找后缀有 file 字样的文件中包含 test
字符串的文件,并打印出该字符串的行。此时,可以使用如下命令:
grep test *file
结果如下所示:
$ grep test test*#查找前缀有“test”的文件包含“test”字符串的文件
testfile1:This a Linux testfile!#列出testfile1 文件中包含test字符的行
testfile_2:Thisis 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
$ 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:Linuxis a free unix-type opterating system.