<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"><generator uri="https://gohugo.io/" version="0.101.0">Hugo</generator><title type="html">awk on A Scripter's Notes</title><subtitle type="html">Emacs, scripting and anything text oriented.</subtitle><link href="https://scripter.co/categories/awk/" rel="alternate" type="text/html" title="HTML"/><link href="https://scripter.co/categories/awk/index.xml" rel="alternate" type="application/rss+xml" title="RSS"/><link href="https://scripter.co/categories/awk/atom.xml" rel="self" type="application/atom+xml" title="Atom"/><link href="https://scripter.co/categories/awk/jf2feed.json" rel="alternate" type="application/jf2feed+json" title="jf2feed"/><updated>2026-04-22T08:24:57-04:00</updated><author><name>Kaushal Modi</name><email>kaushal.modi@gmail.com</email></author><id>https://scripter.co/categories/awk/</id><entry><title type="html">How to remove duplicate lines using awk?</title><link href="https://scripter.co/how-to-remove-duplicate-lines-using-awk/?utm_source=atom_feed" rel="alternate" type="text/html"/><id>https://scripter.co/how-to-remove-duplicate-lines-using-awk/</id><published>2014-03-20T16:11:24-04:00</published><updated>2014-03-20T16:11:24-04:00</updated><content type="html"><![CDATA[<p>If  you type <code>echo &quot;Hi\nHow\nHi\nAre\nHi\nYou?\nAre&quot;</code>, you will get this in your terminal:</p>
<pre tabindex="0"><code>Hi
How
Hi
Are
Hi
You?
Are
</code></pre><p>Here&rsquo;s how we can remove the duplicate lines using <code>awk</code> ..</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-tcsh" data-lang="tcsh"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;Hi\nHow\nHi\nAre\nHi\nYou?\nAre&#34;</span> |  awk <span class="s1">&#39;\!x[$0]++&#39;</span>
</span></span></code></pre></div><p>The above will give this output:</p>
<pre tabindex="0"><code>Hi
How
Are
You?
</code></pre><p>The escape char <code>\</code> is required for <code>!</code> in tcsh.</p>
<p>This is how that awk snippet works:</p>
<ul>
<li>Initially the x array will be empty.</li>
<li>When $0 is <code>Hi</code>, <code>x[$0]=x[Hi]=0</code>. So <code>!x[Hi]</code> will be <code>True</code> and it will be printed out.</li>
<li>After that the <code>x[Hi]</code> becomes 1 because of the <code>++</code> increment operator.</li>
<li>Next time when <code>$0==Hi</code>, as <code>x[Hi]==1</code>, <code>!x[Hi]</code> will be <code>False</code> and so $0 won&rsquo;t be printed out.</li>
</ul>]]></content><category scheme="https://scripter.co/categories/awk" term="awk" label="awk"/><category scheme="https://scripter.co/categories/duplicate" term="duplicate" label="duplicate"/><category scheme="https://scripter.co/categories/tcsh" term="tcsh" label="tcsh"/></entry><entry><title type="html">One liner if-else in awk</title><link href="https://scripter.co/one-liner-if-else-in-awk/?utm_source=atom_feed" rel="alternate" type="text/html"/><id>https://scripter.co/one-liner-if-else-in-awk/</id><published>2014-03-18T16:00:53-04:00</published><updated>2014-03-18T16:00:53-04:00</updated><content type="html"><![CDATA[<p>Tiny snippets showing if/else use in <code>awk</code> ..</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">echo</span> abc:def <span class="p">|</span> awk -F: <span class="s1">&#39;{ if ( $2 ) {print $2} else {print} }&#39;</span>
</span></span></code></pre></div><p>Prints <code>def</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">echo</span> abc <span class="p">|</span> awk -F: <span class="s1">&#39;{ if ( $2 ) {print $2} else {print} }&#39;</span>
</span></span></code></pre></div><p>Prints <code>abc</code>.</p>]]></content><category scheme="https://scripter.co/categories/awk" term="awk" label="awk"/><category scheme="https://scripter.co/categories/condition" term="condition" label="condition"/><category scheme="https://scripter.co/categories/oneliner" term="oneliner" label="oneliner"/></entry><entry><title type="html">Writing aliases with optional arguments in tcsh</title><link href="https://scripter.co/writing-aliases-with-optional-arguments-in-tcsh/?utm_source=atom_feed" rel="alternate" type="text/html"/><id>https://scripter.co/writing-aliases-with-optional-arguments-in-tcsh/</id><published>2014-03-12T11:46:49-04:00</published><updated>2014-03-12T11:46:49-04:00</updated><content type="html"><![CDATA[<p>Some times I would need to define an alias in tcsh which can have
optional arguments. tcsh doesn&rsquo;t seem to support that directly.</p>
<p>Here&rsquo;s how I solve that problem.</p>
<p>If you have an alias <code>alias test 'echo \!:1*'</code> and if you run <code>test abc def</code>, you will get the output <code>abc def</code>.</p>
<p><code>!:1*</code> prints out all the arguments starting from argument 1 till the
last where even argument 1 is optional.  If that argument doesn&rsquo;t
exist, the variable will be assigned a null value.</p>
<p><strong>But</strong> tcsh will not complain about it &ndash; the <code>*</code> after <code>!:1</code> is
the beauty. On the other hand, if I have an alias <code>alias test2 'echo \!:1'</code>, and if I run <code>test</code> &ndash; with zero arguments &ndash; tcsh will give
an error.</p>
<p>So extending that, I have the below alias defined to grab an argument
of any index.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-tcsh" data-lang="tcsh"><span class="line"><span class="cl"><span class="nb">alias </span>opt_args <span class="s1">&#39;set arg1 = `echo \!:1* | awk &#39;</span><span class="s2">&#34;&#39;&#34;</span><span class="s1">&#39;{ print $1 }&#39;</span><span class="s2">&#34;&#39;&#34;</span><span class="s1">&#39;`; \\
</span></span></span><span class="line"><span class="cl"><span class="s1">                echo -n &#34;Arg num 1 = $arg1 &#34;; \\
</span></span></span><span class="line"><span class="cl"><span class="s1">                set arg2 = `echo \!:2* | awk &#39;</span><span class="s2">&#34;&#39;&#34;</span><span class="s1">&#39;{ print $1 }&#39;</span><span class="s2">&#34;&#39;&#34;</span><span class="s1">&#39;`; \\
</span></span></span><span class="line"><span class="cl"><span class="s1">                echo -n &#34;Arg num 2 = $arg2 &#34;; \\
</span></span></span><span class="line"><span class="cl"><span class="s1">                set arg3 = `echo \!:3* | awk &#39;</span><span class="s2">&#34;&#39;&#34;</span><span class="s1">&#39;{ print $1 }&#39;</span><span class="s2">&#34;&#39;&#34;</span><span class="s1">&#39;`; \\
</span></span></span><span class="line"><span class="cl"><span class="s1">                echo -n &#34;Arg num 3 = $arg3 &#34;; \\
</span></span></span><span class="line"><span class="cl"><span class="s1">                echo &#34;&#34;; \\
</span></span></span><span class="line"><span class="cl"><span class="s1">               &#39;</span>
</span></span></code></pre></div><p>You can test this alias by running these commands:</p>
<pre tabindex="0"><code>opt_args abc
opt_args abc def
opt_args abc def ghi
opt_args abc def ghi jkl
</code></pre>]]></content><category scheme="https://scripter.co/categories/tcsh" term="tcsh" label="tcsh"/><category scheme="https://scripter.co/categories/alias" term="alias" label="alias"/><category scheme="https://scripter.co/categories/awk" term="awk" label="awk"/></entry><entry><title type="html">Get current directory name without path</title><link href="https://scripter.co/get-current-directory-name-without-path/?utm_source=atom_feed" rel="alternate" type="text/html"/><id>https://scripter.co/get-current-directory-name-without-path/</id><published>2014-03-04T13:48:01-05:00</published><updated>2014-03-04T13:48:01-05:00</updated><content type="html"><![CDATA[<p>This post shows how to get the current directory name without the
preceeding path, using <code>awk</code> or <code>rev</code>+<code>cut</code> or the boring <code>basename</code>.</p>
<ul>
<li>awk</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="nb">pwd</span> <span class="p">|</span> awk -F/ <span class="s1">&#39;{print $NF}&#39;</span>
</span></span></code></pre></div><ul>
<li>rev and cut</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="nb">pwd</span> <span class="p">|</span> rev <span class="p">|</span> cut -d/ -f <span class="m">1</span> <span class="p">|</span> rev
</span></span></code></pre></div><ul>
<li>basename</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">basename <span class="sb">`</span><span class="nb">pwd</span><span class="sb">`</span>
</span></span></code></pre></div>]]></content><category scheme="https://scripter.co/categories/awk" term="awk" label="awk"/><category scheme="https://scripter.co/categories/shell" term="shell" label="shell"/></entry></feed>