The fact that there's i++
suggests there was either bash
or ksh
shell in use,potentially awk
or perl
as well. In either case, we can use process substitution <(...)
to feed output of xev
to counting loop (although simple pipeline xev | while...
could work just fine).
text processing tools:
Portably and for fewer key strokes we can use awk
:
$ xev | awk '/ButtonPress/{print "ButtonPress",i++}'
ButtonPress 0
ButtonPress 1
ButtonPress 2
ButtonPress 3
perl
version:
$ xev | perl -ne '/ButtonPress/ && printf("ButtonPress:%d\n",++$i)'
ButtonPress:1
ButtonPress:2
ButtonPress:3
Shells:
Here's what works in bash
:
$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf 'ButtonPress: %d\n' "$i";} ;done < <(xev)
ButtonPress: 1
ButtonPress: 2
ButtonPress: 3
In case you don't want spammy output of many lines, we can use printf
to send control code to clear previous line and output only the running count (that is you'd only see integer value change on the line):
$ i=0; while IFS= read -r line; do [[ $line =~ ButtonPress ]] && { ((i++)); printf "\r%b" "\033[2K"; printf 'ButtonPress: %d' "$i";} ;done < <(xev)
Portably in POSIX shell:
$ xev | ( i=0; while IFS= read -r l; do case "$l" in *ButtonPress*) i=$((i+1)) && printf 'ButtonPress:%d\n' "$i";; esac ;done)
ButtonPress:1
ButtonPress:2
ButtonPress:3
basic utils:
For simple, quick, and dirty way we can hack this via cat -n
with line count being printed on the left instead of right:
$ xev | grep --line-buffered 'ButtonPress' | cat -n
1 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
2 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
3 ButtonPress event, serial 34, synthetic NO, window 0x4a00001,
xev | grep -c "ButtonPress"
, showing the number of clicks on exit? – dessert Mar 07 '19 at 09:41ButtonPress
+ number everytime I click on white box pop up window, sorry I'm a novice... – Jackie Nelson Mar 07 '19 at 09:47xev
. How to make it return live value ? – Jackie Nelson Mar 07 '19 at 09:54