The easiest way is if you can get there from the filename. For instance, if the selected filename is /mnt/home/$USER/shell_logs/20140326.log
, you could get to another file in the same directory with a different extension, or another file in another directory pretty easily.
Let's first look at stripping the extension off:
timings="/mnt/home/$USER/shell_logs/20140326.timing" # just for testing
log="${timings%%.*}.log"
echo $log
And here's how to strip the leading path to change to a different directory:
log="/mnt/home/$USER/timings/${timings##*/}"
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,
Best Answer
Using
awk
in a single folderfind
andawk
if you need all files in the current folder and in the subfolders. You can replace.
with your desired folder.or, as getline is bad (thx @fedorqui)
Example