一、概述:
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
1.1 基本格式
grep [OPTIONS] PATTERN [FILE...] // options 是参数
常见参数: -c 统计 -n 行号 -i 忽略大小 -A 搜素到行的后几行也显示 -B 后几行显示
-v 反选 ( ps -ef | grep java | grep -v grep )
二、基本范例:
grep -c "\n" /etc/passwd //查看有多少换行符的count
grep -i "root" /etc/ssh/sshd_config //查看多少root忽略大小写
grep -n "root" /etc/ssh/sshd_config //查询并且显示行号
grep -n -A2 "root" /etc/passwd //查询root,并且把root所在行的后2行也显示。
三、正则范例
grep -n "^root" /etc/passwd //开头等于root
grep -n "bash$" /etc/passwd //结尾等于
grep -n ".root.." /etc/passwd // .表示任意字符
grep -n "^[abcd]" /etc/passwd //开头匹配括号任意字符
grep -n "^[a-d]" /etc/passwd //开头匹配a-d之间,即abcd
本文介绍了grep这一强大的文本搜索工具,它能使用正则表达式搜索文本并打印匹配行。文中说明了grep的基本格式及常见参数,如 -c 统计、-n 显示行号等,还给出了基本范例和正则范例,展示了grep在不同场景下的使用方法。
1122

被折叠的 条评论
为什么被折叠?



