网站首页 > 技术文章 正文
概述
grep命令用来根据指定的模式对目标文件进行逐行匹配检查,并打印匹配到的行。
用法
grep [opation] [pattern] file
pattern可以试特定的字符串或者正则表达式
-v 排除匹配结果
-i 忽略大小写
-w 只匹配指定的字符串
-o 只输出匹配的字符串
-n 输出行号
-E 同egrep命令
正则表达式的使用
grep中正则表达式有三种语法,basic, extended和perl类型,GUN版本的grep中,basic和extended在功能上没有区别,即提供了相同的功能,但是个别特殊字符的使用上会有不同,perl此处不做介绍。
可用的正则表达式元字符
A regular expression may be followed by one of several repetition operators:
? The preceding item is optional and matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times. This is a GNU extension.
{n,m} The preceding item is matched at least n times, but not more than m times.
basic和extended使用上的注意项
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).
Traditional egrep did not support the { meta-character, and some egrep implementations support \\{ instead, so portable scripts should avoid { in grep -E patterns and should use [{] to match a literal {.
GNU grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an invalid interval specification. For example, the command grep -E '{1' searches for the two-character string
{1 instead of reporting a syntax error in the regular expression. POSIX allows this behavior as an extension, but portable scripts should avoid it.
例子
[root@lab ~]# cat output.log
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy roat --minlen=6 --minquality=1 --notstrict --nochanges --notempty
ROOT
[root@lab ~]# grep -o "root" output.log
root
[root@lab ~]# grep -wn "root" output.log
1:pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@lab ~]# grep "[0-9]" output.log
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy roat --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@lab ~]# grep -v "roat" output.log
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
ROOT
[root@lab ~]# grep -i "root" output.log
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
ROOT
[root@lab ~]# grep "^#34; output.log //打印空行
猜你喜欢
- 2024-09-10 oracle实用sql分享:杀进程、长时间操作等等
- 2024-09-10 走在前沿的弄潮儿,怎能不会Git的那些奇技淫巧
- 2024-09-10 TCP“三次握手,四次挥手”你真的懂吗?
- 2024-09-10 制作 deb 软件包(如何制作deb包)
- 2024-09-10 详解虚拟化之KVM概念、架构、功能、常用工具及部署
- 2024-09-10 Linux find命令一定要知道这些(linux find命令的使用)
- 2024-09-10 inux 文本处理三剑客--grep/sed/awk
- 2024-09-10 db2入门必看命令清单--日常运维必需
- 2024-09-10 浅谈Linux中的&&和ll(linux中atime)
- 2024-09-10 浅谈Linux中的&&和ll,补充&和l
- 1508℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 520℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 490℃MySQL service启动脚本浅析(r12笔记第59天)
- 469℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 467℃启用MySQL查询缓存(mysql8.0查询缓存)
- 447℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 427℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 424℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)