bash – Is there a tool for collapsing output of a command base on regex?

Let’s say I have a command that outputs the state of some application. This command runs forver (or until C-c) and outputs a single line when something happens inside the application. It outputs the following format

State N: Stuff M happened

I want a program, let’s call it collapse_prog, that takes the lines outputed the command, based on the regex I give to it, to overwrite the previous state. For example, let’s say the input regex is State (\d+), I run the following command

my_command | collapse_prog

and the my_command outputs

State 1: Stuff A happened
State 2: Stuff B happened

then the collapse_prog should display as is, but after the my_command outputs another line

State 1: Stuff C happened

then the collapse_prog should have the following output

State 1: Stuff C happened
State 2: Stuff B happened

Is there such a program out there already?

Read more here: Source link