如何修复awk在多个条件语句中两次打印同一行?



我有一个日志文件看起来像这样:

Jun 29 16:46:13 iPhone SomeThing[MULTILINE] <Notice>: [AppName] file.x:115 DEBUG: ClassNumberOne viewControllers: (
"<UINavigationController: 0x105031a00>",
"<UINavigationController: 0x10505ba00>",
"<UINavigationController: 0x10486fe00>",
"<UINavigationController: 0x105052600>",
"<UINavigationController: 0x105065c00>"
)
Jun 29 16:46:13 someline that should not be captured
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:151 DEBUG: ClassNumberTwo ARG2 2
Jun 29 16:46:13 someline that should not be captured
Jun 29 16:46:13 iPhone SomeThing[MULTILINE] <Notice>: [AppName] file.x:152 DEBUG: ClassNumberOne viewControllers: {
"<UINavigationController: 0x105031a00>",
"<UINavigationController: 0x10505ba00>",
"<UINavigationController: 0x10486fe00>",
"<UINavigationController: 0x105052600>",
"<UINavigationController: 0x105065c00>"
}
Jun 29 16:46:13 someline that should not be captured
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:153 DEBUG: ClassNumberTwo ARG2 2

我的命令是打印包含DEBUG:的行,无论是单行日志(出于解释目的在示例日志中标记为单行(,还是以(开头直到)的多行(标记为多行(,或以{开头并以}结尾。下面是代码:

awk 'f; /}}/{f=0} /)/{f=0} /DEBUG:/{print; f=/{||(/}' file.txt

我的预期输出与本文顶部提供的输入(不包括Jun 29 16:46:13 someline that should not be captured等不相关的行(相同,但是我得到的是单行的重复项:

Jun 29 16:46:13 iPhone SomeThing[MULTILINE] <Notice>: [AppName] file.x:115 DEBUG: ClassNumberOne viewControllers: (
"<UINavigationController: 0x105031a00>",
"<UINavigationController: 0x10505ba00>",
"<UINavigationController: 0x10486fe00>",
"<UINavigationController: 0x105052600>",
"<UINavigationController: 0x105065c00>"
)
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:151 DEBUG: ClassNumberTwo ARG2 2
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:151 DEBUG: ClassNumberTwo ARG2 2
Jun 29 16:46:13 iPhone SomeThing[MULTILINE] <Notice>: [AppName] file.x:152 DEBUG: ClassNumberOne viewControllers: {
"<UINavigationController: 0x105031a00>",
"<UINavigationController: 0x10505ba00>",
"<UINavigationController: 0x10486fe00>",
"<UINavigationController: 0x105052600>",
"<UINavigationController: 0x105065c00>"
}
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:153 DEBUG: ClassNumberTwo ARG2 2
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:153 DEBUG: ClassNumberTwo ARG2 2

知道如何解决这个问题吗?谢谢。

$ awk 'f{print; if (/^[)}]/) f=0} /DEBUG:/{if (/[({]$/) f=1; print}' file
Jun 29 16:46:13 iPhone SomeThing[MULTILINE] <Notice>: [AppName] file.x:115 DEBUG: ClassNumberOne viewControllers: (
"<UINavigationController: 0x105031a00>",
"<UINavigationController: 0x10505ba00>",
"<UINavigationController: 0x10486fe00>",
"<UINavigationController: 0x105052600>",
"<UINavigationController: 0x105065c00>"
)
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:151 DEBUG: ClassNumberTwo ARG2 2
Jun 29 16:46:13 iPhone SomeThing[MULTILINE] <Notice>: [AppName] file.x:152 DEBUG: ClassNumberOne viewControllers: {
"<UINavigationController: 0x105031a00>",
"<UINavigationController: 0x10505ba00>",
"<UINavigationController: 0x10486fe00>",
"<UINavigationController: 0x105052600>",
"<UINavigationController: 0x105065c00>"
}
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:153 DEBUG: ClassNumberTwo ARG2 2

您可以使用此awk

awk '/DEBUG:/{print; f = /[({]$/; next}; f; /^[)}]/{f = 0}' file.log

Jun 29 16:46:13 iPhone SomeThing[MULTILINE] <Notice>: [AppName] file.x:115 DEBUG: ClassNumberOne viewControllers: (
"<UINavigationController: 0x105031a00>",
"<UINavigationController: 0x10505ba00>",
"<UINavigationController: 0x10486fe00>",
"<UINavigationController: 0x105052600>",
"<UINavigationController: 0x105065c00>"
)
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:151 DEBUG: ClassNumberTwo ARG2 2
Jun 29 16:46:13 iPhone SomeThing[MULTILINE] <Notice>: [AppName] file.x:152 DEBUG: ClassNumberOne viewControllers: {
"<UINavigationController: 0x105031a00>",
"<UINavigationController: 0x10505ba00>",
"<UINavigationController: 0x10486fe00>",
"<UINavigationController: 0x105052600>",
"<UINavigationController: 0x105065c00>"
}
Jun 29 16:46:13 iPhone SomeThing[SINGLE LINE] <Notice>: [AppName] file.x:153 DEBUG: ClassNumberTwo ARG2 2

相关内容

  • 没有找到相关文章

最新更新