根据内容删除两行之间的文本块 [英] remove block of text between two lines based on content
问题描述
我需要删除/过滤一个非常大的日志文件我设法将日志文件放入包含 <--
或 -->
的行开始的文本块中,以包含 Content- 的行结束长度:
现在,如果此文本块包含单词REGISTER
,则需要将其删除.
I need to remove/filter a very large log file
i managed to bring the log-file into blocks of text starting with a line containing <--
or -->
ending with a line containing Content-Length:
now if this block of text contains the word REGISTER
it need to be deleted.
我找到了流动的例子:
# sed script to delete a block if /regex/ matches inside it
:t
/start/,/end/ { # For each line between these block markers..
/end/!{ # If we are not at the /end/ marker
$!{ # nor the last line of the file,
N; # add the Next line to the pattern space
bt
} # and branch (loop back) to the :t label.
} # This line matches the /end/ marker.
/regex/d; # If /regex/ matches, delete the block.
} # Otherwise, the block will be printed.
#---end of script---
由 Russell Davies 在此页面
written by Russell Davies on this page
但我不知道如何将其传输到单行语句以在管道中使用我的目标是通过管道将日志文件的 tail -F
传送到最终版本,以便每分钟更新
but i do not know how to transport this to a single line statement to use in a pipe
my goal is to pipe a tail -F
of the log file to the final version so it get updates by the minute
推荐答案
试试这个:
awk '/<--|-->/{rec=""; f=1} f{rec = rec $0 ORS} /Content-Length:/{ if (f && (rec !~ "REGISTER")) printf "%s",rec; f=0}' file
如果它没有做您想要的,请提供更多关于您想要的信息以及示例输入和输出.
If it doesn't do what you want, provide more info on what you want along with sample input and output.
为了分解上述内容,以下是单独行的每个语句,并带有一些注释:
To break down the above, here's each statement on separate lines with some comments:
awk '
/<--|-->/ {rec=""; f=1} # find the start of the record, reset the string to hold it and set a flag to indicate we've started processing a record
f {rec = rec $0 ORS} # append to the end of the string containing the current record
/Content-Length:/{ # find the end of the record
if (f && (rec !~ "REGISTER")) # print the record if it doesn't contain "REGISTER"
printf "%s",rec
f=0 # clear the "found record" indicator
}
' file
如果您想要打印的记录之间有文本,只需为未设置的找到"标志添加一个测试,并调用打印当前记录的默认操作 (!f;强>)
and if you have text between your records that you'd want printed, just add a test for the "found" flag not being set and invoke the default action of printing the current record (!f;)
awk '/<--|-->/{rec=""; f=1} f{rec = rec $0 ORS} !f; /Content-Length:/{ if (f && (rec !~ "REGISTER")) printf "%s",rec; f=0}' file
这篇关于根据内容删除两行之间的文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!