Linux grep命令是一款文本搜索神器,它可以在文件或者文本中搜索符合某种特定格式的字符串,并返回结果。它是一个强大的命令行工具,可以帮助用户快速搜索文本,是Linux系统中常用的文本处理工具之一。
使用方法
1、基本格式:grep [options] pattern file
2、搜索文件内容:grep [options] pattern file
3、搜索目录下所有文件:grep [options] pattern directory
4、搜索多个文件:grep [options] pattern file1 file2 file3
5、搜索文件夹下所有文件:grep [options] pattern directory/*
选项
- -i:忽略大小写,即搜索时不区分大小写
- -v:反向搜索,即搜索不符合条件的行
- -n:显示匹配行及其行号
- -c:只输出匹配行的数量
- -l:只输出匹配文件的文件名
- -h:不显示文件名
实例
[root@localhost ~]# cat file1.txt this is a test hello world [root@localhost ~]# grep -i hello file1.txt hello world
上面的例子中,使用grep -i hello file1.txt命令,搜索file1.txt文件中包含hello字符串的行,-i表示忽略大小写,搜索出文件中包含hello字符串的行,即hello world。