user3341592 user3341592 - 1 year ago 66
Bash Question

Catch the absence of a pattern (Updated) following another one (INSERT / UPDATE / DELETE)

How could we detect error cases in log files such as:

[DEBUG][2016-08-02 17:11:22,047][DbConn] - [INSERT INTO table1 ...]
[DEBUG][2016-08-02 17:11:22,461][DbConn] - [Updated: 16282 records]
[DEBUG][2016-08-02 17:11:22,465][DbConn] - [ UPDATE table2 SET ...]
[DEBUG][2016-08-02 17:11:22,616][DbConn] - [Updated: 3 records]
[DEBUG][2016-08-02 17:11:22,620][DbConn] - [ DELETE FROM table3 WHERE ...]
[DEBUG][2016-08-02 17:11:22,786][DbConn] - [Updated: 1 records]
[DEBUG][2016-08-02 17:11:23,723][DbConn] - [INSERT INTO table4 ...]
[DEBUG][2016-08-02 17:11:24,442][DbConn] - [SELECT * FROM table5 ...]

where the absence of
Updated: X records
shows that the request wasn't correctly executed?

In this example, line 7 (
INSERT INTO table4 ...
) should be outputted as something wrong happened when executing that SQL request.

I'm sure it's almost possible to write a one-liner for this, but I don't see the right way to begin. Any tip?

Answer Source

Assuming we expect Updated every even numbered line,

awk 'NR%2==0{if (!/Updated/) print a} {a=$0}' log

A more generic one:

awk 'NR>1{if (!/Updated/ && a ~ /INSERT|UPDATE|DELETE/) print a} {a=$0}' log
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download