Answer to question 2
This type of scenario certainly requires some sort of startswith/endswith logic because your field values seem to repeat too frequently. (There could be a timing related fix, e.g. using maxpause or maxspan , but you'll have to decide if that is possible with your data or not. See link at bottom of this post.)
I think I found a working solution by using just "startswith" without using "endswith" (the keepevicted=true doesn't seem to matter in this test case.) I did some playing around with your provided sample data (I copied and pasted into a temp file and used splunk to load it with the file command without indexing it 😉 splunk rocks!)
| file /tmp/examplefile.txt | sort -_time | rex "ip=(?<ip>\d+)" | rex "\s(?<router>\w+)\s\[" | transaction router,ip startswith="aaaaaaa" | eval my_closed_txn=if(searchmatch("bbbbbbb"),1,0)
The first 3 search commands are needed to attempt to match your environment; you can pretty much ignore them.
Side note:
BTW, Simply using searchmatch is not 100% accurate here. This is because we are not ensuring that it is the last event, we are only making sure that the text string "bbbbbbb" exists within the combined transaction event's text (the _raw field). So this can be a problem if "bbbbbbb" occurs as the second event of 3, this approach will not catch that. You can work around this by using match instead of searchmatch in combination with a multiline regex that explicitly will match the bbbbbbb on the last line only. Of the top of my head (aka untested), you could probably use an expression like this:
| eval my_closed_txn=if(match(_raw, "(?ms)^.*[\r\n][^\r\n]+\bbbbbbbb\b(?:[\r\n]+|$)",1,0)
In case your not a regex guru, " \b " means boundary, which is even more confusing when you are already matching "b"s. 😉
So this approach would be more accurate, but often it's not worth the hassle. (I have used this approach when analyzing FTP transactions to see if the last session event was a "successful logout", so this kind of thing is needed from time to time.)
Make sure you note this Q&A: Does combining startswith and maxspan in a transaction work?
Hope this helps.
... View more