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