localhost
localhost

Reputation: 1273

How to keep refreshing screen with continuous text

I'm trying to debug using a program that keeps outputting formatted text such as:

r/c 0123456789ABCDEF
00: 0000011100000000
01: 0000011100000000
02: 0000011100000000
03: 0000011100000000
04: 0000011100000000
05: 0000011100000000
06: 0000011100000000
07: 0000000000000000
08: 0001000000000000
09: 0000000000000000
0A: 0000000000000000
0B: 0000000000000000
0C: 0000000000000000
0D: 0011000000000000

r/c 0123456789ABCDEF
00: 0000011100000000
01: 0000011100000000
02: 0000011100000000
03: 0000011100000000
04: 0000011100000000
05: 0000011100000000
06: 0000011100000000
07: 0000000000000000
08: 0001000000000000
09: 0000000000000000
0A: 0000000000000000
0B: 0000000000000000
0C: 0000000000000000
0D: 0011000000000000

...etc

It's showing the output from a keyboard matrix, so I would like to see how the numbers change as I am pressing keys on the keyboard that I am testing.

The problem is that it flies by on the console so fast, it's just a big mess of text. I would like to insert a screen clear between every group, so I can actually see where the keys that I am pressing make changes in the matrix. The program that I am running is sudo ./hid_listen.

I want to see the changes in realtime, as if I pipe this output into a file, it is very hard to tell with thousands of lines of text where exactly it was that I was pressing a particular key. I'm thinking something like top that keeps refreshing itself, but of course much simpler.

Upvotes: 0

Views: 237

Answers (3)

Mark Setchell
Mark Setchell

Reputation: 207425

Please do not accept this as your answer because it is based entirely on the excellent suggestion of @anubhava. I just thought it may help you out because you say there are thousands of lines of confusing text.

Basically, it is the same as his, except that it remembers the last line seen (in the variable "x") and only prints output when it changes - so you have less stuff to wade through:

yourcommand | awk -F: '/^r\/c /{system("clear");print $0;x=""}$2!=x{print $0;x=$2}'

Output:

r/c 0123456789ABCDEF
00: 0000011100000000
07: 0000000000000000
08: 0001000000000000
09: 0000000000000000
0D: 0011000000000000

Upvotes: 0

anubhava
anubhava

Reputation: 785058

You can pipe your command to awk to force screen clear every time a blank line shows up (start of a new group):

sudo ./hid_listen | awk '/^$/{system("clear")} 1'

Alternatively you can use this command to insert a clear command every time r/c line comes:

sudo ./hid_listen | awk '/^r\/c /{system("clear")} 1'

Upvotes: 5

localhost
localhost

Reputation: 1273

Found it myself,

sudo ./hid_listen | sed -e 's/^$/'$(echo -en "\033c")'/'

Probably terminal specific, but it worked for me. echo -en "\033c turns into ESC-c, which clears the screen. Although I haven't tried it, anubhava's answer looks like what I was looking for as well.

Upvotes: 1

Related Questions