awk打印匹配的行和匹配的行 [英] awk print matching line and line before the matched
本文介绍了awk打印匹配的行和匹配的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我尝试使用awk进行的操作.获取与正则表达式匹配的行,以及与之匹配并打印之前的行.我可以得到与正则表达式匹配的行,但不能得到紧接在它之前的行:
Following is what I am trying to do using awk. Get the line that matches the regex and the line immediately before the matched and print. I can get the line that matched the regex but not the line immediately before that:
awk '{if ($0!~/^CGCGGCTGCTGG/) print $0}'
推荐答案
在这种情况下,您可以使用grep轻松解决它:
In this case you could easily solve it with grep:
grep -B1 foo file
但是,如果您需要使用awk:
However, if you need to to use awk:
awk '/foo/{if (a && a !~ /foo/) print a; print} {a=$0}' file
这篇关于awk打印匹配的行和匹配的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文