<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>duplicate on
A Scripter's Notes</title><link>https://scripter.co/categories/duplicate/</link><description>Recent content in duplicate
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/duplicate/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></channel></rss>