<?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">duplicate on A Scripter's Notes</title><subtitle type="html">Emacs, scripting and anything text oriented.</subtitle><link href="https://scripter.co/categories/duplicate/" rel="alternate" type="text/html" title="HTML"/><link href="https://scripter.co/categories/duplicate/index.xml" rel="alternate" type="application/rss+xml" title="RSS"/><link href="https://scripter.co/categories/duplicate/atom.xml" rel="self" type="application/atom+xml" title="Atom"/><link href="https://scripter.co/categories/duplicate/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/duplicate/</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></feed>