每天一个linux命令(grep)

grep----文本搜索工具

格式:grep [参数]

主要功能:

1.匹配某个文件中的某段内容

2.与ps连用,作为ps的搜索条件

主要参数:

-a:不忽略二进制数据

-c:统计总共匹配出多少行

[root@root home]# cat 1.txt
123123
123123
123123
[root@root home]# grep –c “11.txt
3

-n:将搜索出来的结果前面添加上行号

[root@root home]# grep –n “31.txt
1:123123
2:123123
3:123123

-r :明确要求搜索文件夹下的子目录

[root@root home]# grep1” test/*
test/1.txt:123123
test/1.txt:123123
test/1.txt:123123
test/2.txt:123123123123123123123123123123123
grep: test/a: Is a directory --提示test/a是个目录
grep: test/b: Is a directory --提示test/b是个目录
[root@root home]# grep –r “1” test/*
test/1.txt:123123
test/1.txt:123123
test/1.txt:123123
test/2.txt:123123123123123123123123123123123
test/a/3.txt:321321

-l:显示匹配到内容的文件名(-L:显示没有匹配到的文件名)

[root@root home]# grep -l –r “123”  test/*
test/1.txt
test/2.txt
[root@root home]# grep -l –r “1”  test/*
test/1.txt
test/2.txt
test/a/3.txt

正则表达式:


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~华丽的切割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

原文地址:https://www.cnblogs.com/hollyhock/p/10182166.html