仅显示带有另一个pattern2的行后面的pattern1的行

时间:2014-03-22 08:31:01

标签: bash

我尝试从objump过滤转储,而我目前仍在过滤某些所需的行。

我现在有这样的事情......

many lines with pattern1 I dont need
line I don't need with pattern1
line I don't need with pattern1
line with pattern1 I need because the NEXT LINE matches the pattern2
  line with pattern2
line with pattern1 I need because the NEXT LINE matches the pattern2
  line with pattern2
  line with pattern2
line I don't need with pattern1
line I don't need with pattern1
many lines with pattern1 I dont need

所以问题是,我需要删除每一行,包括带有pattern2的行,而* 只保留pattern2后面带有pattern1 的行。我需要以通用的方式做到这一点。 (我正在制作一个剧本)。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这个awk应该做你想做的事情

 $ awk '/pattern1/{k=$0;next}/pattern2/&&k{print k};{k=0}' file
line with pattern1 I need because the NEXT LINE matches the pattern2
line with pattern1 I need because the NEXT LINE matches the pattern2

我们在变量pattern1中存储与k匹配的行,并跳过其余的命令

如果某一行符合pattern2且变量k不为零,那么我们会打印存储的行。

如果行与pattern1

不匹配,我们将变量设置为零

答案 1 :(得分:0)

您可以尝试此sed

sed -n '/pattern1/{N; /\n.*pattern2/!{D;b};p}'