1.5 KiB
| title | date | tags | |
|---|---|---|---|
| bash function and awk | 2018-05-31 00:00:48 |
|
I'll come across many hg branch-switching or log searching tasks during my work. Using bash functions and awk greatly reduce the time I spend on dealing with these tasks. So let's see what these functions can do to help me improve my productivity.
bash functions
Bash functions are scripts like alias, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as && I guess:
function run {
source ~/Develop/django/bin/activate
./manage.py runserver
}
I activate django virtural enviroment first and then run django serve.
Functions, like in any other languages, can take parameters and returns a result. So I can use this ability to do a liitle more complex work:
function ba { hgba | grep "$1" }
# hgba is an alias for hg branches
I can just type ba release then get current release branch info.
Append:
I made some updates to the ba function and make it auto copying the branch result to my clipboard:
function ba { var="$(hgba | grep $1)" && echo $var | awk -F ':' END{print} | awk -F ':' '{print $NF}' | tr -d '\n' | pbcopy && echo $var }
awk
awk is a command I just know recently. I need to process a bunch of logs and analyze them. What I used to do is download the logs, grep things I want into a txt file and then process the txt file with python.