带有多个字符串和通配符的 Grep

发布于 2025-01-13 03:20:33 字数 790 浏览 4 评论 0原文

我正在尝试从具有多个字符串和通配符的日志文件中获取匹配项。这就是日志的样子

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜清冷一曲。 2025-01-20 03:20:33

这个awk可以解决这个问题:

awk 'BEGIN {FS="\\|"; pat["abc"]; pat["ijk"]}
$1 in pat && /:sessionId=/ {print; delete pat[$1]}' file.log

abc|07Jan2016:sessionId=F4DF
ijk|06Jan2016:sessionId=CF38

代码演示

This awk may solve the problem:

awk 'BEGIN {FS="\\|"; pat["abc"]; pat["ijk"]}
$1 in pat && /:sessionId=/ {print; delete pat[$1]}' file.log

abc|07Jan2016:sessionId=F4DF
ijk|06Jan2016:sessionId=CF38

Code Demo

一抹淡然 2025-01-20 03:20:33

对于显示的示例,请尝试执行以下 awk 代码。一旦每个字符串的第一次出现之一被打印出来,这将从程序中退出,因此这不会读取整个Input_file。

awk -F'|' '
($1=="abc" || $1=="xyz") && ++arr[$1]==1{
   count++
   print
}
count==2{ exit }
' Input_file

说明:为上述代码添加详细说明。

awk -F'|' '                                ##Starting awk program from here and setting field separator as | here.
($1=="abc" || $1=="xyz") && ++arr[$1]==1{  ##Checking condition if 1st field is abc OR xyz AND their respective index in array arr is ONLY 1.
   count++                                 ##Increase count with 1 here.
   print                                   ##Printing current line here.
}
count==2{ exit }                           ##Checking condition if count is 2 then exit from program.
' Input_file                               ##Mentioning Input_file name here.

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.

awk -F'|' '
($1=="abc" || $1=="xyz") && ++arr[$1]==1{
   count++
   print
}
count==2{ exit }
' Input_file

Explanation: Adding detailed explanation for above code.

awk -F'|' '                                ##Starting awk program from here and setting field separator as | here.
($1=="abc" || $1=="xyz") && ++arr[$1]==1{  ##Checking condition if 1st field is abc OR xyz AND their respective index in array arr is ONLY 1.
   count++                                 ##Increase count with 1 here.
   print                                   ##Printing current line here.
}
count==2{ exit }                           ##Checking condition if count is 2 then exit from program.
' Input_file                               ##Mentioning Input_file name here.
紫罗兰の梦幻 2025-01-20 03:20:33

使用 grep 建议:

grep -E "^(abc|ijk)\|" file.log

使用 awk 建议:

 awk '/^(abc|ijk)\|/1' file.log

Suggestion with grep:

grep -E "^(abc|ijk)\|" file.log

Suggesting with awk:

 awk '/^(abc|ijk)\|/1' file.log
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文