Perl 如果包含某个单词且在括号内,则提取多行输出
我有以下方面的意见:Perl 如果包含某个单词且在括号内,则提取多行输出,perl,awk,grep,Perl,Awk,Grep,我有以下方面的意见: Jun 29 16:46:13 iPhone SomeThing[79987] <Notice>: [AppName] file.x:115 DEBUG: ClassNumberOne viewControllers: ( "<UINavigationController: 0x105031a00>", "<UINavigationController: 0x10505ba00>", "<UINavigati
Jun 29 16:46:13 iPhone SomeThing[79987] <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[79987] <Notice>: [AppName] file.x:151 DEBUG: ClassNumberTwo ARG2 2
Jun 29 16:46:13[79987]:[AppName]文件.x:115调试:ClassNumberOne视图控制器:(
"",
"",
"",
"",
""
)
6月29日16:46:13 iPhone SomeThing[79987]:[AppName]file.x:151调试:ClassNumberTwo ARG2
这里有两种情况需要匹配,一种是多线的,另一种是单线的。标准是它必须具有DEBUG:
关键字。对于多行,如果该行包含关键字and和(
),则该行应匹配到)
。每行由换行符分隔。我想不出来。目前我正在使用一个简单的grep调试:
,就是这样。但对于多行场景,除了第一行之外,所有内容都丢失了。我不熟悉perl,知道吗?提前谢谢
请注意,我使用的是iOS(越狱),因此另一个工具可能会受到限制
编辑:
预期输出将是与标准匹配的整行,这与上面显示的输入示例相同。实际输入中有大量其他行没有关键字
DEBUG:
,因此将被忽略。下面是一个在Perl中使用Regexp的示例(但这可能应该由类似的解析器更准确地处理):
使用功能qw(比如说);
严格使用;
使用警告;
my$data=do{local$/;};
my@lines=$data
=~ /
^(?:(?!$)*调试:
(?:
(?: [^(]*? $ )
|
(?: (?:(?!$).)* \( [^)]* \) .*? $ )
)
)/gmsx;
代表@行;
在每个UNIX机箱的任何shell中使用任何awk:
$ awk 'f; /\)/{f=0} /DEBUG:/{print; f=/\(/}' file
Jun 29 16:46:13 iPhone SomeThing[79987] <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[79987] <Notice>: [AppName] file.x:151 DEBUG: ClassNumberTwo ARG2 2
您显示的输入的预期输出是什么?此外,您输入的文件中没有花括号。这与示例中显示的输入相同,因为不必使用关键字的实际输入将被忽略。简单地说,我想过滤掉包含DEBUG:关键字(multiline或singleline)的内容。如何将单行与多行区分开来?对于多行情况,它在哪里结束?只有一个右括号(
)
?对不起,刚才也编辑了!是的,这意味着你可以使用一个表达式,比如你能将你使用的代码添加到问题中吗?我没想到awk
能做到这一点,特别是当需要满足两个条件(即单行和多行)时,这是一个非常好的解决方案,因为我打算在终端上使用它。如果你不介意的话,你能简单解释一下语法吗?好吧,我在每条语句旁边添加了一个解释作为伪代码。在UNIX中,为了简单、清晰、可移植性等,如果需要执行s/old/new
,请使用sed;如果你需要做“代码> G/Re/P,那么使用GRIP,对于任何其他文本操作都使用AWK,并且对于不仅仅是文本操作的任何操作,例如操作(创建/销毁/移动)文件、操作进程、套接字、调用其他工具等,否则您将在shell中进行操作,然后考虑Perl、Python、Ruby等。
$ awk 'f; /\)/{f=0} /DEBUG:/{print; f=/\(/}' file
Jun 29 16:46:13 iPhone SomeThing[79987] <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[79987] <Notice>: [AppName] file.x:151 DEBUG: ClassNumberTwo ARG2 2
awk ' # WHILE read line DO
f; # IF the flag `f` is set THEN print the current line ENDIF
/\)/{f=0} # IF the current line contains `)` THEN clear the flag ENDIF
/DEBUG:/ { # IF the current line contains `DEBUG:` THEN
print; # print the line
f=/\(/ # set the flag `f` to 1 if the line contains `(`, 0 otherwise
} # ENDIF
' file # ENDWHILE