19 lines
4.6 KiB
HTML
19 lines
4.6 KiB
HTML
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><meta name="generator" content="Hexo 6.0.0"><link rel="alternate" href="/atom.xml" title="MarkDown" type="application/atom+xml">
|
||
</head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2018/05/31/bash-function-and-awk/" class="post-title-link">bash function and awk</a></h2><div class="post-meta"><div class="post-time">2018年5月31日</div></div><div class="post-content"><p>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.</p>
|
||
<h4 id="bash-functions"><a href="#bash-functions" class="headerlink" title="bash functions"></a>bash functions</h4><p>Bash functions are scripts like <code>alias</code>, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as <code>&&</code> I guess:</p>
|
||
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> run {</span><br><span class="line"> <span class="built_in">source</span> ~/Develop/django/bin/activate</span><br><span class="line"> ./manage.py runserver</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>I activate django virtural enviroment first and then run django serve.</p>
|
||
<p>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:</p>
|
||
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> ba { hgba | grep <span class="string">"<span class="variable">$1</span>"</span> }</span><br><span class="line"><span class="comment"># hgba is an alias for hg branches</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<p>I can just type <code>ba release</code> then get current release branch info.</p>
|
||
<hr>
|
||
<p>Append:</p>
|
||
<p>I made some updates to the <code>ba</code> function and make it auto copying the branch result to my clipboard:</p>
|
||
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">function</span> ba { var=<span class="string">"<span class="subst">$(hgba | grep $1)</span>"</span> && <span class="built_in">echo</span> <span class="variable">$var</span> | awk -F <span class="string">':'</span> END{<span class="built_in">print</span>} | awk -F <span class="string">':'</span> <span class="string">'{print $NF}'</span> | tr -d <span class="string">'\n'</span> | pbcopy && <span class="built_in">echo</span> <span class="variable">$var</span> }</span><br></pre></td></tr></table></figure>
|
||
|
||
|
||
|
||
<h4 id="awk"><a href="#awk" class="headerlink" title="awk"></a>awk</h4><p><code>awk</code> 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. </p>
|
||
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script></body></html> |