- grep命令用于对文本进行搜索,格式为“grep [选项] [文件]”
- 搜索某个关键词:"grep 关键词 文本文件"
- 参数说明
-b 将可执行文件当做文本文件来搜索
-c 仅显示找到的次数
-i 忽略大小写
-n 显示行号
-v 反向选择
文本文件:
[root@localhost testA]# cat 111.txt
Welcome to linuxprobe.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[root@localhost testA]#
进行搜索:
[root@localhost testA]# grep Linux 111.txt
Free Linux Lessons
Linux Course
[root@localhost testA]# grep -n Linux 111.txt
3:Free Linux Lessons
5:Linux Course
[root@localhost testA]# grep -i -n Linux 111.txt
1:Welcome to linuxprobe.com
3:Free Linux Lessons
5:Linux Course
[root@localhost testA]# grep -i -n -v Linux 111.txt
2:Red Hat certified
4:Professional guidance
[root@localhost testA]# grep -i -n -v -c Linux 111.txt
2
- 实例2:查找111.txt中不包含大写或小写linux的行,并输出到同目录下的123.txt文件中
[root@localhost testA]# grep -n -v -i linux 111.txt >> /root/testA/123.txt
[root@localhost testA]# cat 123.txt
2:Red Hat certified
4:Professional guidance