{"id":3748,"date":"2024-01-25T19:52:13","date_gmt":"2024-01-26T00:52:13","guid":{"rendered":"https:\/\/osric.com\/chris\/accidental-developer\/?p=3748"},"modified":"2024-01-25T19:52:40","modified_gmt":"2024-01-26T00:52:40","slug":"minor-improvements-to-legacy-perl-code","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2024\/01\/minor-improvements-to-legacy-perl-code\/","title":{"rendered":"Minor improvements to legacy Perl code"},"content":{"rendered":"<p>We&#8217;re always working with code we didn&#8217;t write. You&#8217;ll spend far more time looking at code you didn&#8217;t write (or don&#8217;t remember writing) than you will spend writing new code.<\/p>\n<p>Today I looked at an example Perl script that used 45 lines of code to pull the company associated with an OUI (Organizationally Unique Identifier) from a text file, given a MAC address.<\/p>\n<p>I thought I could do slightly better.<\/p>\n<p><strong>find_mac_co.sh:<\/strong><\/p>\n<pre><code>#!\/bin\/sh\r\nOUI=$(echo \"$1\" | sed 's\/[^A-Fa-f0-9]\/\/g' | cut -c1-6)\r\nawk -F \"\\t\" -v IGNORECASE=1 -v OUI=\"$OUI\" '$0 ~ OUI { print $3 }' ouidb.tsv\r\nexit 0<\/code><\/pre>\n<p><strong>Example run:<\/strong><\/p>\n<pre><code>$ sh find_mac_co.sh 7c:ab:60:ff:ff:ff\r\nApple, Inc.<\/code><\/pre>\n<p>There&#8217;s probably a way to make the Perl version shorter too. I&#8217;m more familiar with bash and shell commands.<\/p>\n<p>The biggest problem with this script is that it relies on an up-to-date list of OUIs. An even better way is to query an API:<\/p>\n<p><strong>find_mac_co_api.sh<\/strong><\/p>\n<pre><code>#!\/bin\/sh\r\nMACADDRESS=\"$1\"\r\ncurl \"https:\/\/api.maclookup.app\/v2\/macs\/$MACADDRESS\/company\/name\"\r\nexit 0<\/code><\/pre>\n<p><strong>Example run:<\/strong><\/p>\n<pre><code>$ sh find_mac_co_api.sh 7c:ab:60:ff:ff:ff\r\nApple, Inc.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;re always working with code we didn&#8217;t write. You&#8217;ll spend far more time looking at code you didn&#8217;t write (or don&#8217;t remember writing) than you will spend writing new code. Today I looked at an example Perl script that used 45 lines of code to pull the company associated with an OUI (Organizationally Unique Identifier) &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2024\/01\/minor-improvements-to-legacy-perl-code\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Minor improvements to legacy Perl code<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,232],"tags":[197,215,360],"class_list":["post-3748","post","type-post","status-publish","format-standard","hentry","category-perl","category-tips-tricks","tag-bash","tag-curl","tag-perl"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=3748"}],"version-history":[{"count":6,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3748\/revisions"}],"predecessor-version":[{"id":3756,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3748\/revisions\/3756"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=3748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=3748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=3748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}