Print a line only if the next line does NOT contain a particular match

Here is an awk alternative:

awk '
  /^Starting/ { I[$5] = $0                  }
  /^ID/       { delete I[$2]                }
  END         { for (key in I) print I[key] }
' infile

Output:

Starting activity for ID 33367

The I associative array keeps track of what ids have been seen.


sed '$!N;/\n.*completed/d;P;D' <input

This will delete from output all input lines which are not followed by a line matching the string completed.


Here's how you could do it with GNU sed:

sed -r 'N; /([0-9]+)\n\w+\s+\1/d; P; D' infile
  • N reads one more line into pattern space.
  • The match regex checks if identical ids are found, if so pattern space is deleted (d) and the cycle is restarted.
  • If it didn't match, print out the first line in pattern space (P) and delete it (D).

Tags:

Grep

Awk

Sed