- ## bash function and awk

  2018年5月31日

  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:

      1
      2
      3
      4

      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:

      1
      2

      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:

      1
      2

      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.