Linux命令—egrep

gzming -
Linux命令—egrep

作用:寻找匹配指定模式的文本.

命令格式

egrep Options "regex pattern" InputFile1 InputFile2 ...

egrep 等价于 grep -E.

示例输入:

[ming@localhost test]$ cat data.txt 
abcd123
456789
efgh123
987654
123abcd
[ming@localhost test]$ cat data1.txt 
abcd123
456789
efgh123
987654
[ming@localhost test]$ cat data2.txt 
abcd123
456789
efgh123
987654
[ming@localhost test]$ cat data3.txt 
123456
ABCD123

1. 输出匹配模式的行

[ming@localhost test]$ egrep "^[a-z]+" data.txt 
abcd123
efgh123

[ming@localhost test]$ egrep "[a-z]+" data1.txt data2.txt 
data1.txt:abcd123
data1.txt:efgh123
data2.txt:abcd123
data2.txt:efgh123

输出匹配行的行号:-n

[ming@localhost test]$ egrep -n "^[a-z]+" data.txt 
1:abcd123
3:efgh123

[ming@localhost test]$ egrep -n "[a-z]+" data1.txt data2.txt 
data1.txt:1:abcd123
data1.txt:3:efgh123
data2.txt:1:abcd123
data2.txt:3:efgh123

只输出匹配的行数:-c

[ming@localhost test]$ egrep -c "^[a-z]+" data.txt 
2

2. 只输出匹配模式的内容: -o

[ming@localhost test]$ egrep -o "^[a-z]+" data.txt 
abcd
efgh

3. 反向匹配: -v

输出不匹配的行、行数:

[ming@localhost test]$ egrep -v "^[a-z]+" data.txt 
456789
987654
123abcd

[ming@localhost test]$ egrep -vc "^[a-z]+" data.txt 
3

4. 输出包括匹配行的文件: -l

[ming@localhost test]$ egrep -l "[a-z]+" data1.txt data2.txt data3.txt
data1.txt
data2.txt

5. 输出不包括匹配行的文件: -L

[ming@localhost test]$ egrep -L "[a-z]+" data1.txt data2.txt data3.txt 
data3.txt

6. 忽略大小写: -i

[ming@localhost test]$ egrep -i "[a-z]+" data3.txt 
ABCD123

7. 指定多个模式: -e

[ming@localhost test]$ egrep -e "^[a-z]+" -e "[0-9]+" data3.txt 
123456
ABCD123

[ming@localhost test]$ egrep "^[a-z]+ | [0-9]+" data3.txt 
123456
ABCD123

8. 搜索目录

递归搜索目录下的文件:-r

[ming@localhost ~]$ egrep -r "^[a-z]+" test
...
test/data1.txt:abcd123
test/data1.txt:efgh123
test/data2.txt:abcd123
test/data2.txt:efgh123
test/main1.c:int main()
test/main2.c:int main()
test/data.txt:abcd123
test/data.txt:efgh123

排除指定的目录:--exclude-dir

[ming@localhost ~]$ egrep -r "^[0-9]+" . --exclude-dir ..
...
./test/data1.txt:456789
./test/data1.txt:987654
./test/data2.txt:456789
./test/data2.txt:987654
./test/data.txt:456789
./test/data.txt:987654
./test/data.txt:123abcd
./test/data3.txt:123456
./.vboxclient-clipboard.pid:2364
./.vboxclient-seamless.pid:2376
./.vboxclient-draganddrop.pid:2383
./.vboxclient-display-svga-x11.pid:2386

排除指定的文件:--exclude

[ming@localhost ~]$ egrep -r "^[a-z]+" test --exclude *.c --exclude *.cpp
...
test/data1.txt:abcd123
test/data1.txt:efgh123
test/data2.txt:abcd123
test/data2.txt:efgh123
test/data.txt:abcd123
test/data.txt:efgh123

只包含指定的文件:--include

[ming@localhost ~]$ egrep -r "^[a-z]+" . --include *.c --include *.cpp
./test/main1.c:int main()
./test/main2.c:int main()

9. 从文件中读取模式: -f

[ming@localhost test]$ cat patterns.re
^[a-z]+
^[0-9]+

[ming@localhost test]$ egrep -f patterns.re data.txt 
abcd123
456789
efgh123
987654
123abcd
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。
上一篇: Linux之alias命令

Tags 标签

linuxgrepegrep

扩展阅读

加个好友,技术交流

1628738909466805.jpg