-
使用格式
grep [选项] “模式” [文件] -
常用选项使用
不加选项,在指定文件夹中查找指定字符串,返回查找字符串的一整行。
grep "hello" ./Desktop/test
返回:
hello world!
hello China!
选项:-i, 不区分字符串大小写查找。
grep -i "hEllo" ./Desktop/test
返回:
hello world!
hello China!
HellO
选项:-n, 返回结果显示行号
grep -n "hello" ./Desktop/test
返回:
1:hello world!
6:hello China!
选项:-v,返回非指定查找的内容
grep -v "hello" ./Desktop/test
返回:
ennnnasdsa
sadsad
asdasfasfa
asdas
asd
asda
HellO
sadasd
asdasd
选项:-c,返回查找到字符串的总数量
grep -c "hello" ./Desktop/test
返回:
2
选项:-A n,返回查找到字符串以及后n行的内容
grep -A 2 "hello" ./Desktop/test
返回:
hello world!
ennnnasdsa
sadsad
--
hello China!
asd
asda
选项:-B n,返回查找到字符串以及前n行的内容
grep -B 2 "hello" ./Desktop/test
返回:
hello world!
--
asdasfasfa
asdas
hello China!
选项:-C n, 返回查找到字符串以及前后n行的内容
grep -C 2 "hello" ./Desktop/test
返回:
hello world!
ennnnasdsa
sadsad
asdasfasfa
asdas
hello China!
asd
asda