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

查看:104
本文介绍了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.

我的日志文件都包含了不同的协议PKTS和它们的内容与他们的价值观确定了他们不同的协议字段的文本日志OSPF协议去codeS的打印输出。我想处理这些文件并打印出的日志只有特定的线路,涉及到具体的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中可以很容易地检测出图案的多线组合,但您需要创建一个所谓的的状态机 的在code识别序列。

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程序的下半年的,只有当它是直接美元的一个的今年上半年的行pceded p $。 (使用更复杂的状态机,你可以检测模式的任意序列)。

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

现在,这个例子太简单了,只勉强的状态机。有趣的国家只对持续时间持续了的如果的语句和preceding状态是隐含的,这取决于的价值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天全站免登陆