检查一行不是以grep开头的特定字符串 [英] Check if a line does not start with a specific string with grep

查看:790
本文介绍了检查一行不是以grep开头的特定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件app.log

  Oct 06 03:51:43 test test 
Nov 06 15 :04:53 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测试测试测试

如何将所有不在06年11月开始的行列入grep?



我试过了

  grep -En^ [^ Nov 06] app.log 

我无法获取其中包含06的行。

解决方案

只需使用下面的grep命令,

  grep -v'^ Nov 06'file 

grep - 帮助

  -v,--invert-match选择不匹配的行

另一个通过正则表达式的破解,

  grep -P'^(?! Nov 06)'file 

正则表达式解释:


  • ^

  • (?! Nov 06)这个负面预测表明:在行开始之后没有字符串 Nov 06 。如果是,则匹配边界在每行的第一个字符之前存在。



另一个基于正则表达式的解决方案通过PCRE动词 (* SKIP)(* F)

  grep -P'^ Nov 06(* SKIP)(* F)| ^'file 


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

How do I grep all the lines that does not begin with Nov 06 ?

I have tried

grep -En "^[^Nov 06]" app.log

I am not able to get lines which have 06 in them.

解决方案

Simply use the below grep command,

grep -v '^Nov 06' file

From grep --help,

-v, --invert-match        select non-matching lines

Another hack through regex,

grep -P '^(?!Nov 06)' file

Regex Explanation:

  • ^ Asserts that we are at the start.
  • (?!Nov 06) This negative lookahead asserts that there isn't a string Nov 06 following the line start. If yes, then match the boundary exists before first character in each line.

Another regex based solution through PCRE verb (*SKIP)(*F)

grep -P '^Nov 06(*SKIP)(*F)|^' file

这篇关于检查一行不是以grep开头的特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆