Where are my emails stored and how can I mark them as read from commandline?
I suspect that your answer here will be mail rules, and AppleScript. Rather than relying on parsing out the raw messages, you can create a rule to filter on the bad status messages and then run an AppleScript, and another on the Ok.

To learn about AppleScript and the Mail dictionary: enter Introduction to Scripting Mail

Mail rules are set in Preferences on the Rules tab.

AppleScripts for mail are installed in $HOME/Library/Application Scripts/com.apple.mail/ Helpfully, when you choose AppleScript from the rules dropdown it'll give you the option to open that folder.

You can probably accomplish this with a single script, run when an OK message is received, to check the inbox for bad messages and mark them as read, along with the current message.

Assuming you mean shell scripting by command line, which is comparable to AppleScript - if anything AppleScript is easier, being designed to be English-like and quite readable.

To view the available commands in AS for Mail.app you can open Script Editor (Applications/Utilities), then File->Open Dictionary. Open the Mail dictionary for the command reference guide.

Alternatively, you can try this the *nix way here, but, on a Mac, the Mac way is often easier.