Linux 三剑客 之 grep
grep 擅长查找功能
(一)用法:grep [参数] PATTERN [FILE...]
常用参数:
-v 显示不被pattern匹配到的行
-i 忽略字符大小写
-n 显示匹配的行号
-c 统计匹配的行数
-o 仅显示匹配到的字符串
-E 使用ERE.相当于egrep
更多内容可在linux系统中输入 man grep 查看 解释
(二)常用文本查询命令示例:
grep 'HELLO' test.txt --在test.txt 找到包含"HELLO"字符的行内容(区分大小写)
grep -i 'HELLO' test.txt --在test.txt 找到包含"HELLO"字符的行内容(不区分大小写,即包含"Hello""hello""HEllo"等均能被找出)
grep -n 'HELLO' test.txt --在test.txt 找到包含"HELLO"字符的行内容(区分大小写),并展示行内容所在行号。
grep -i -n 'HELLO' test.txt --在test.txt 找到包含"HELLO"字符的行内容(不区分大小写),并展示行内容所在行号。
(三)拓展内容
grep -n ^s test.txt --查找以s开头的行及行号
grep -n n$ test.txt --查找以n结尾的行及行号
grep -Bx 'hello' test.txt --查找包含'hello' 及hello之前x行的行
grep -Ax 'hello' test.txt --查找包含'hello' 及hello之后x行的行
grep -Cx 'hello' test.txt --查找包含'hello'及'hello'之前x行以及hello之后x行的行
grep -v 'hello' test.txt --查找不包含'hello'的行
grep -w 'hello' test.txt --查找完全匹配'hello'单词的行,例如’hellostr word‘或’strhello word‘均不会被查出,只有’hello world‘此类会被查出。
grep -w 'hello' test.txt |wc -l --统计'hello' 单词在test中出现的行数
grep -o -w 'hello' test.txt |wc -l --统计'hello' 单词在test中出现的次数
学习文章链接:
https://www.zsythink.net/archives/1733