使用 grep 检查一行是否不以特定字符串开头 [英] Check if a line does not start with a specific string with grep
本文介绍了使用 grep 检查一行是否不以特定字符串开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件 app.log
I have a file app.log
Oct 06 03:51:43 test test
Nov 06 15:04:53 text text text
more text more text
Nov 06 15:06:43 text text text
Nov 06 15:07:33
more text more text
Nov 06 15:14:23 test test
more text more text
some more text
Nothing but text
some extra text
Nov 06 15:34:31 test test test
如何 grep 所有不是以 Nov 06 开头的行?
How do I grep all the lines that does not begin with Nov 06 ?
我试过了
grep -En "^[^Nov 06]" app.log
我无法获取包含 06 的行.
I am not able to get lines which have 06 in them.
推荐答案
只需使用下面的 grep 命令,
Simply use the below grep command,
grep -v '^Nov 06' file
来自 grep --help
,
-v, --invert-match select non-matching lines
通过正则表达式的另一个黑客,
Another hack through regex,
grep -P '^(?!Nov 06)' file
正则表达式解释:
^
断言我们处于开始阶段.(?!Nov 06)
这个否定前瞻断言在行开始之后没有字符串Nov 06
.如果是,则匹配每行中第一个字符之前存在的边界.
^
Asserts that we are at the start.(?!Nov 06)
This negative lookahead asserts that there isn't a stringNov 06
following the line start. If yes, then match the boundary exists before first character in each line.
通过 PCRE 动词的另一种基于正则表达式的解决方案 (*SKIP)(*F)
Another regex based solution through PCRE verb (*SKIP)(*F)
grep -P '^Nov 06(*SKIP)(*F)|^' file
这篇关于使用 grep 检查一行是否不以特定字符串开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文