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..?
CodePudding user response:
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
CodePudding user response:
Suggestion with grep
:
grep -E "^(abc|ijk)\|" file.log
Suggesting with awk
:
awk '/^(abc|ijk)\|/1' file.log