grep for Error 并打印包含错误上下两个字符串的所有行 [英] grep for Error and print all the lines containing 2 strings above and below Error
本文介绍了grep for Error 并打印包含错误上下两个字符串的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
saaa vcahJJ HKak vk
Import xxx xXXXXX xxxx
aaaa aaaa aaaa ffffff
hhhhhh hhhhhh hhh hhh hhhhhh
Error reading readStatus api
aaa hhhh aaa aaaaa
gggggggg ggggg xxxxxxxxxx
uuuu hhhhhhhh fffffffff
query run ends
qidIdih II v iQE Iqe
我想在包含上述日志的文件中找到 'Error'
字符串,然后打印 2 个字符串 'Import'
和 ' 之间的所有可用信息结束'
.如何使用 grep/sed
执行此操作试过 这个但没有得到太多.注意:我不知道前后会有多少行.它可能与我提供的上述样本不同
I want to find the 'Error'
string in the file containing above logs and then print all the info available between 2 strings 'Import'
and 'ends'
.
How can I do this using grep/sed
Tried this
but didn't get much.
Note: I dont know how many lines will be before and after. It may vary from above sample I have provided
推荐答案
你可以试试这个 sed
You can try this sed
sed '/^Import/!d;:A;N;/\nImport/{h;s/.*\n//;bA};/ends$/!bA;h;s/\nError//;tB;d;:B;x' infile
说明:
sed '
/^Import/!d # if a line start with Import
:A
N # get an other line
/\nImport/{h;s/.*\n//;bA} # if the last line start with Import
# keep only this last line and return to A
/ends$/!bA # If the last line end with ends
h # keep all the lines in the hold space
s/\nError// # look for a line which start with Error
tB # if find jump to B
d # not find, delete all and return to the start
:B
x # restore all the lines and print
' infile
这篇关于grep for Error 并打印包含错误上下两个字符串的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文