【问题标题】:Continuing a awk or sed print including a keyword until an end pattern is reached继续包含关键字的 awk 或 sed 打印,直到达到结束模式
【发布时间】:2017-02-22 18:03:18
【问题描述】:

我有大量长而不规则的日志,如下所示:

###<date> errortext <errorcode-xxxxx> 
errortext 
errortext 
errortext 
errortext
###<date> errortext <errorcode-yyyy>
errortext 
errortext 
###<date> errortext <errorcode-<zzzzzzz>
errortext 
errortext 
errortext 
errortext 
errortext 
errortext 
errortext 

长度不规则,需要使用grep/awk/sed或类似方法查找相同错误码的错误。

我需要将这些文档按错误代码拆分,将一个代码的所有错误打印到一个文档中。

当我尝试使用如下行查找整个错误代码段时:

sed -n '/#</{:start /###/!{N;b start};/<errorcode-024332>/p}' file

上述行的问题在于它只会打印包含“errorcode-024332”的行,而不是所有错误代码,直到下一段开始(在这种情况下使用分隔符“###”)。

我如何做到这一点?

【问题讨论】:

标签: bash shell unix awk sed


【解决方案1】:

您的问题发生是因为#&lt;### 都匹配“标题”行,因此您只打印它而从不循环。您还附加到模式缓冲区而不是逐行使用,因此标题总是会匹配。

假设你想显示“errorcode-024332”的“header”和“errortext”,我会这样做:

sed -n '/#<.*<errorcode-024332>/{:start p;n;/###/!{b start}}'
  1. 当我们匹配到与我们的错误代码对应的标题行时
  2. 我们打印出来
  3. 我们得到下一行
  4. 如果下一行不包含###,则返回第2步。

我对您的样本数据进行了快速测试:

$ echo "###<date> errortext <errorcode-xxxxx>
errortext
errortext
[...]
errortext
errortext " | sed -n '/#<.*<errorcode-yyyy>/{:start p;n;/###/!{b start}}'

###<date> errortext <errorcode-yyyy>
errortext
errortext

【讨论】:

  • 像这样添加我的关键字:sed -n '/#{:start N;/###/!{b start};//p}' 文件给出我的结果与我的旧命令相同。我误解了把这个放在哪里吗?
  • @Flowdorio 我已经编辑过了,如果它回答了你的问题,请告诉我。
  • 确实如此!谢谢!
【解决方案2】:

你可以使用awk,像这样:

awk -F'[<>-]' '/^#/{f=$(NF-1)}{print >> f; close(f)}' file.log

让我解释为多行版本:

# Using this set of field delimiters it is simple to access
# the error code in the previous last field
BEGIN { FS="[<>-]"}

# On lines which start with a '#'
/^#/ {
    # We set the output (f)ilename to the error code
    f=$(NF-1)
}

# On all lines ...
{
    # ... append current line to (f)ilename
    print >> f;

    # Make sure to close the file to avoid running out of
    # file descriptors in case there are many different error
    # codes. If you are not concerned about that, you may
    # comment out this line.
    close(f)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2019-08-31
    • 2019-07-01
    • 2016-10-01
    • 2022-01-16
    相关资源
    最近更新 更多