<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://teaching.healthtech.dtu.dk/22126/index.php?action=history&amp;feed=atom&amp;title=Zip_codes_answers</id>
	<title>Zip codes answers - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://teaching.healthtech.dtu.dk/22126/index.php?action=history&amp;feed=atom&amp;title=Zip_codes_answers"/>
	<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22126/index.php?title=Zip_codes_answers&amp;action=history"/>
	<updated>2026-05-03T19:24:41Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://teaching.healthtech.dtu.dk/22126/index.php?title=Zip_codes_answers&amp;diff=18&amp;oldid=prev</id>
		<title>WikiSysop: Created page with &quot; Please note that in UNIX, there is more than one way to do things.  &#039;&#039;&#039;Q1:&#039;&#039;&#039; &lt;pre&gt; zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz  |awk &#039;BEGIN{FS=&quot;,&quot;}{if($5==&quot;\&quot;NY\&quot;&quot;){print $0}}&#039;|wc -l &lt;/pre&gt; &lt;ol&gt; &lt;li&gt;&lt;code&gt;zcat&lt;/code&gt;: Decompresses the ZIP_CODES.csv.gz file and outputs its content.&lt;/li&gt; &lt;li&gt;&lt;code&gt;awk &#039;BEGIN{FS=&quot;,&quot;}&lt;/code&gt;: Sets the field separator to a comma for the CSV file.&lt;/li&gt; &lt;li&gt;&lt;code&gt;if($5==&quot;\&quot;NY\&quot;&quot;){print $0}&lt;/code&gt;: Checks if the 5th field (st...&quot;</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk/22126/index.php?title=Zip_codes_answers&amp;diff=18&amp;oldid=prev"/>
		<updated>2024-03-19T15:24:30Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; Please note that in UNIX, there is more than one way to do things.  &amp;#039;&amp;#039;&amp;#039;Q1:&amp;#039;&amp;#039;&amp;#039; &amp;lt;pre&amp;gt; zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz  |awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}{if($5==&amp;quot;\&amp;quot;NY\&amp;quot;&amp;quot;){print $0}}&amp;#039;|wc -l &amp;lt;/pre&amp;gt; &amp;lt;ol&amp;gt; &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;zcat&amp;lt;/code&amp;gt;: Decompresses the ZIP_CODES.csv.gz file and outputs its content.&amp;lt;/li&amp;gt; &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}&amp;lt;/code&amp;gt;: Sets the field separator to a comma for the CSV file.&amp;lt;/li&amp;gt; &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;if($5==&amp;quot;\&amp;quot;NY\&amp;quot;&amp;quot;){print $0}&amp;lt;/code&amp;gt;: Checks if the 5th field (st...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
Please note that in UNIX, there is more than one way to do things.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q1:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz  |awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}{if($5==&amp;quot;\&amp;quot;NY\&amp;quot;&amp;quot;){print $0}}&amp;#039;|wc -l&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;zcat&amp;lt;/code&amp;gt;: Decompresses the ZIP_CODES.csv.gz file and outputs its content.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}&amp;lt;/code&amp;gt;: Sets the field separator to a comma for the CSV file.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;if($5==&amp;quot;\&amp;quot;NY\&amp;quot;&amp;quot;){print $0}&amp;lt;/code&amp;gt;: Checks if the 5th field (state) is &amp;quot;NY&amp;quot; and prints the entire line.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;wc -l&amp;lt;/code&amp;gt;: Counts the number of lines, which corresponds to the number of ZIP codes in NY.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q2:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz|awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}{if($5==&amp;quot;\&amp;quot;NY\&amp;quot;&amp;quot;){print $4}}&amp;#039; |sort |uniq -c  |sort -nr  |head -n 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;if($5==&amp;quot;\&amp;quot;NY\&amp;quot;&amp;quot;){print $4}&amp;lt;/code&amp;gt;: Selects and prints the ZIP codes of NY.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sort&amp;lt;/code&amp;gt;: Sorts the ZIP codes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;uniq -c&amp;lt;/code&amp;gt;: Counts the unique occurrences of each ZIP code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sort -nr&amp;lt;/code&amp;gt;: Sorts the counts in descending order.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;head -n 3&amp;lt;/code&amp;gt;: Displays the top 3 most frequent ZIP codes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q3:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz |awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}{if($5==&amp;quot;\&amp;quot;NY\&amp;quot;&amp;quot;){print $4}}&amp;#039; |sort |uniq -c  |awk &amp;#039;{if($1==1){print $0}}&amp;#039;  |wc -l&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Processes similar to Q2 to get the count of each ZIP code in NY.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;{if($1==1){print $0}}&amp;#039;&amp;lt;/code&amp;gt;: Filters out ZIP codes that appear only once.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;wc -l&amp;lt;/code&amp;gt;: Counts these unique ZIP codes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q4:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz|awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}{if($5==&amp;quot;\&amp;quot;NY\&amp;quot;&amp;quot;){print $4}}&amp;#039; |sort |uniq -c  |awk &amp;#039;{print $1}&amp;#039; |sort -n |uniq -c&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Initial steps similar to Q2 to count each ZIP code in NY.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;{print $1}&amp;#039;&amp;lt;/code&amp;gt;: Extracts the count number.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sort -n | uniq -c&amp;lt;/code&amp;gt;: Sorts and counts how often each frequency occurs.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q5:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz | awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}{print $2&amp;quot;\t&amp;quot;$4}&amp;#039; |sed &amp;quot;s/+//g&amp;quot; |sed &amp;quot;s/\&amp;quot;//g&amp;quot;   |awk &amp;#039;{if($1&amp;gt;=39.4){print $2}}&amp;#039; |sort |uniq |wc -l&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;{print $2&amp;quot;\t&amp;quot;$4}&amp;#039;&amp;lt;/code&amp;gt;: Extracts latitude and ZIP code, separated by a tab.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sed &amp;quot;s/+//g&amp;quot;&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;sed &amp;quot;s/\&amp;quot;//g&amp;quot;&amp;lt;/code&amp;gt;: Removes pluses and quotes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;{if($1&amp;gt;=39.4){print $2}}&amp;#039;&amp;lt;/code&amp;gt;: Filters ZIP codes with latitude &amp;gt;= 39.4.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sort | uniq | wc -l&amp;lt;/code&amp;gt;: Counts unique ZIP codes that meet the criteria.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q6:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz| awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}{print $4&amp;quot;\t&amp;quot;$5}&amp;#039;   |sort |uniq -c |sed &amp;quot;s/\([0-9]\) /\1\t/g&amp;quot; | sort  -k 3,3 -k 1,1nr -t &amp;quot;`/bin/echo -e &amp;#039;\t&amp;#039;`&amp;quot;  | sort -u -k 3,3 -t &amp;quot;`/bin/echo -e &amp;#039;\t&amp;#039;`&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;zcat&amp;lt;/code&amp;gt;: Decompresses the &amp;lt;code&amp;gt;ZIP_CODES.csv.gz&amp;lt;/code&amp;gt; file and outputs its content.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}&amp;lt;/code&amp;gt;: Sets the field separator to a comma. Prints the 4th (city) and 5th (state) fields separated by a tab.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sort&amp;lt;/code&amp;gt;: Sorts the output alphabetically.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;uniq -c&amp;lt;/code&amp;gt;: Counts unique occurrences of each city-state pair.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sed &amp;quot;s/\([0-9]\) /\1\t/g&amp;quot;&amp;lt;/code&amp;gt;: Replaces the space between the count and city-state pair with a tab for better formatting.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sort -k 3,3 -k 1,1nr -t &amp;quot;\`/bin/echo -e &amp;#039;\t&amp;#039;\`&amp;quot;&amp;lt;/code&amp;gt;: Sorts the output first by state (3rd field), then by the count (1st field) in reverse numeric order, using a tab as the field separator.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sort -u -k 3,3 -t &amp;quot;\`/bin/echo -e &amp;#039;\t&amp;#039;\`&amp;quot;&amp;lt;/code&amp;gt;: Uniquely sorts the list by state, keeping only the first occurrence of each state (which is the city with the highest count due to the previous sort).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Q7:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
zcat /home/projects/22126_NGS/exercises/unix/ZIP_CODES.csv.gz | awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}{print $4}&amp;#039; |sort |uniq  |awk &amp;#039;{print substr($1,0,2)}&amp;#039; |grep &amp;quot;\&amp;quot;M&amp;quot; |wc -l&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;zcat&amp;lt;/code&amp;gt;: Decompresses the &amp;lt;code&amp;gt;ZIP_CODES.csv.gz&amp;lt;/code&amp;gt; file and outputs its content.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;BEGIN{FS=&amp;quot;,&amp;quot;}&amp;lt;/code&amp;gt;: Sets the field separator to a comma. Prints only the 4th field (ZIP code).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;sort&amp;lt;/code&amp;gt;: Sorts the ZIP codes alphabetically.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;uniq&amp;lt;/code&amp;gt;: Filters out duplicate ZIP codes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;awk &amp;#039;{print substr($1,0,2)}&amp;#039;&amp;lt;/code&amp;gt;: Prints the first two characters of each ZIP code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;grep &amp;quot;\&amp;quot;M&amp;quot;&amp;lt;/code&amp;gt;: Filters for lines where the first two characters start with &amp;quot;M&amp;quot; (accounting for any quotes).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;code&amp;gt;wc -l&amp;lt;/code&amp;gt;: Counts the number of lines, giving the total number of unique ZIP codes starting with &amp;quot;M&amp;quot;.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;/div&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
</feed>