在grep中结合-v标志和-A标志 [英] Combining -v flag and -A flag in grep

查看:140
本文介绍了在grep中结合-v标志和-A标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在文件中搜索字符串,删除包含该字符串的所有行,并且删除包含该字符串的任何行后面的两行。我希望我能用这样的东西来完成这件事......

  $ grep -v -A 2 two temp.txt 
one
five
$

...但不幸的是,不行。有没有一个简单的,我可以用grep或其他shell命令做到这一点? 下面的代码适用于GNU sed和OS X。

  $ sed'/ two / {N; N; d;}'temp。 txt 
one
five




  • 查找行匹配 two

  • 多读两行
  • 删除它们


I need to search a file for a string, remove any line that contains the string, and also remove the two lines following any line that contains the string. I was hoping I could accomplish this using something like this...

$ grep -v -A 2 two temp.txt
one
five
$

...but unfortunately this did not work. Is there a simple I can do this with grep or another shell command?

解决方案

The following works both with GNU sed and with OS X.

$ sed '/two/{N;N;d;}' temp.txt
one
five

  • find line matching two
  • read in two more lines
  • delete them

这篇关于在grep中结合-v标志和-A标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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