Reputation: 5771
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..?
Upvotes: 2
Views: 410
Reputation: 133428
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.
Upvotes: 1
Reputation: 4865
Suggestion with grep
:
grep -E "^(abc|ijk)\|" file.log
Suggesting with awk
:
awk '/^(abc|ijk)\|/1' file.log
Upvotes: 0