带有多个字符串和通配符的 Grep
我正在尝试从具有多个字符串和通配符的日志文件中获取匹配项。这就是日志的样子
test.log
abc|07Jan2016:sessionId=F4DF
<<random log lines>>
def|08Jan2016:sessionId=5415
<<random log lines>>
abc|08Jan2016:sessionId=F4DF
<<random log lines>>
xyz|09Jan2016:sessionId=F3D2
<<random log lines>>
ijk|06Jan2016:sessionId=CF38
我期待的结果
abc|07Jan2016:sessionId=F4DF
ijk|06Jan2016:sessionId=CF38
如您所见,我只想从字符串匹配 'abc' 的行中获取具有 sessionIds 的日志行,并且'ijk'
我尝试过的 grep 命令
grep -m 1 'abc.*sessionId\|ijk.*sessionId' test.log
我得到的结果
ijk|06Jan2016:sessionId=CF38
grep 不是在寻找与字符串 'abc' 的匹配,而是在寻找与通配符 '.*sessionId' 匹配的 'ijk' 有人可以吗让我知道我在这里缺少什么吗..?
I'm trying to get matches from a log file with multiple strings and a wildcard. This is how the log looks like
test.log
abc|07Jan2016:sessionId=F4DF
<<random log lines>>
def|08Jan2016:sessionId=5415
<<random log lines>>
abc|08Jan2016:sessionId=F4DF
<<random log lines>>
xyz|09Jan2016:sessionId=F3D2
<<random log lines>>
ijk|06Jan2016:sessionId=CF38
The result I'm expecting
abc|07Jan2016:sessionId=F4DF
ijk|06Jan2016:sessionId=CF38
As you can see, I just want to get the log lines that have sessionIds from lines that have the string matches 'abc' and 'ijk'
The grep command that I tried
grep -m 1 'abc.*sessionId\|ijk.*sessionId' test.log
The result I'm getting
ijk|06Jan2016:sessionId=CF38
The grep is not looking for matches with the string 'abc', but it is looking for the 'ijk' match with the wildcard '.*sessionId' Can somebody please let me know what I'm missing here..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个
awk
可以解决这个问题:代码演示
This
awk
may solve the problem:Code Demo
对于显示的示例,请尝试执行以下
awk
代码。一旦每个字符串的第一次出现之一被打印出来,这将从程序中退出,因此这不会读取整个Input_file。说明:为上述代码添加详细说明。
With your shown samples, please try following
awk
code. This will exit from program once one of each string's very 1st occurrence is printed, so this will not read whole Input_file.Explanation: Adding detailed explanation for above code.
使用
grep
建议:使用
awk
建议:Suggestion with
grep
:Suggesting with
awk
: