awk 模式可以匹配多行吗? [英] Can awk patterns match multiple lines?

查看:27
本文介绍了awk 模式可以匹配多行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些复杂的日志文件,我需要编写一些工具来处理它们.我一直在玩 awk,但我不确定 awk 是否适合于此.

I have some complex log files that I need to write some tools to process them. I have been playing with awk but I am not sure if awk is the right tool for this.

我的日志文件是 OSPF 协议解码的打印输出,其中包含各种协议 pkt 的文本日志及其内容,其各种协议字段以其值标识.我想处理这些文件并仅打印出与特定 pkts 相关的日志的某些行.每个 pkt 日志可以由该 pkt 条目的不同行数组成.

My log files are print outs of OSPF protocol decodes which contain a text log of the various protocol pkts and their contents with their various protocol fields identified with their values. I want to process these files and print out only certain lines of the log that pertain to specific pkts. Each pkt log can consist of a varying number of lines for that pkt's entry.

awk 似乎能够处理与模式匹配的单行.我可以找到所需的 pkt,但随后我需要匹配后续行中的模式,以确定它是否是我想要打印的 pkt.

awk seems to be able to process a single line that matches a pattern. I can locate the desired pkt but then I need to match patterns in the lines that follow in order to determine if it is a pkt I want to print out.

另一种看待这个问题的方法是,我想隔离日志文件中的几行,并根据几行上的模式匹配打印出作为特定 pkt 详细信息的那些行.

Another way to look at this is that I would want to isolate several lines in the log file and print out those lines that are the details of a particular pkt based on pattern matches on several lines.

由于 awk 似乎是基于行的,我不确定这是否是最好的工具.

Since awk seems to be line-based, I am not sure if that would be the best tool to use.

如果awk能做到这一点,是怎么做到的?如果没有,关于为此使用哪种工具的任何建议?

If awk can do this, how it is done? If not, any suggestions on which tool to use for this?

推荐答案

Awk 可以轻松检测模式的多行组合,但是您需要创建所谓的状态机 用于识别序列.

Awk can easily detect multi-line combinations of patterns, but you need to create what is called a state machine in your code to recognize the sequence.

考虑这个输入:

how
second half #1
now
first half
second half #2
brown
second half #3
cow

如您所见,识别单一模式很容易.现在,我们可以编写一个 awk 程序,该程序仅在 后半部分 前面直接有前半部分 行时才能识别它.(使用更复杂的状态机,您可以检测任意模式序列.)

As you have seen, it's easy to recognize a single pattern. Now, we can write an awk program that recognizes second half only when it is directly preceded by a first half line. (With a more sophisticated state machine you could detect an arbitrary sequence of patterns.)

/second half/ {
  if(lastLine == "first half") {
    print
  }
}

{ lastLine = $0 }

如果你运行这个,你会看到:

If you run this you will see:

second half #2

现在,这个例子非常简单,几乎只是一个状态机.有趣的状态仅在 if 语句的持续时间内持续,并且前面的状态是隐式的,具体取决于 lastLine 的值. 在更规范的状态机中,您将保留一个显式状态变量和状态到状态的转换取决于现有状态和当前输入.但您可能不需要那么多控制机制.

Now, this example is absurdly simple and only barely a state machine. The interesting state lasts only for the duration of the if statement and the preceding state is implicit, depending on the value of lastLine. In a more canonical state machine you would keep an explicit state variable and transition from state-to-state depending on both the existing state and the current input. But you may not need that much control mechanism.

这篇关于awk 模式可以匹配多行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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