I need to view a specific line of a log file from time to time:
$ head -10 log.txt|tail -1 # to view line 10 of log.txt
Then I wrote a function v
in my bashrc
to make life easier:
$ v 10
Ok, maybe I'm a little splitting hair here: I'd like to ignore the space too:
$ v10
The only way I know is to define lots of alias:
alias v1='v 1'
alias v2='v 2'
alias v3='v 3'
alias v4='v 4'
...
Is there any good way for this?
Thanks @Chirlo and @anishsane for the idea.
Here is my final version, based on @anishsane's with some fixes:
eval "`declare -f command_not_found_handle | sed s/command_not_found_handle/command_not_found_handle_orig/`"
command_not_found_handle(){
if expr match "$1" "v[0-9][0-9]*" >/dev/null ; then
v ${1:1}
return $?
fi
command_not_found_handle_orig "$@"
}
This is not an answer, but a hint:
When you give some command on ubuntu, & if that program is not installed, then the shell calls its own handler, saying
The program 'something' is currently not installed. You can install it by typing:
sudo apt-get install something
You can try finding that piece of program, which does this. Then hook your code, which would look if command is matching regex v[0-9]*
& then execute v $lineNumber
based on some parsing of command...
Based on chepner's comment, here is the update:
Add below in your .bashrc:
eval "`declare -f command_not_found_handle | sed s/command_not_found_handle/command_not_found_handle_orig/`"
command_not_found_handle(){
if expr match "$1" "v[0-9]*" >/dev/null ; then
v ${1:1}
return $?
fi
command_not_found_handle_orig "$[@]"
}
Check the command_not_found_handle () if you have bash
> 4, you can override it to handle this case. When you type v10
and the bash can't resolve it, this function will be called with the v10
as the first parameter, and you catch it and do your thing there.