{"id":3967,"date":"2015-12-10T07:58:45","date_gmt":"2015-12-10T07:58:45","guid":{"rendered":"http:\/\/really.zonky.org\/?p=3967"},"modified":"2015-12-11T20:18:45","modified_gmt":"2015-12-11T20:18:45","slug":"getting-the-maximum-of-a-set-of-numbers","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=3967","title":{"rendered":"Getting The Maximum Of A Set Of Numbers"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-3856\" src=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt-300x200.png?resize=300%2C200\" alt=\"damascus-unix-prompt\" width=\"300\" height=\"200\" srcset=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png?resize=300%2C200&amp;ssl=1 300w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png?w=792&amp;ssl=1 792w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>You have a a column of numbers that you have produced in some manner such as :-<\/p>\n<pre>$ <strong>awk '\/clean message\/ {print $(NF-1)}' mail.info.log<\/strong>\r\n...\r\n100935\r\n12197\r\n3606\r\n84653\r\n4498\r\n99110\r\n4762\r\n3001\r\n10889\r\n12611\r\n12249\r\n12245\r\n136599\r\n49097\r\n6668\r\n<\/pre>\n<p>And you want a quick and dirty way of finding the largest number. Well there\u00a0<em>is<\/em> a way but it is perhaps the least efficient way to do it, and that is to sort the numbers into numerical order and use &#8220;head&#8221; to display the first one :-<\/p>\n<pre>$ <strong>awk '\/clean message\/ {print $(NF-1)}' mail.info.log | sort -rn | head -1<\/strong>\r\n5476168\r\n<\/pre>\n<p>But frankly there must be a better method. And yes there is if you happen to be using <a href=\"http:\/\/www.zsh.org\/\">zsh<\/a> (or possibly others, but this has been tested with zsh).\u00a0Simply iterate over the values assigning the current value to the &#8220;max&#8221; variable if the current variable is larger :-<\/p>\n<pre>$ <strong>max=0; for x in $(awk '\/clean message\/ {print $(NF-1)}' mail.info.log); [[ $x -gt $max ]] &amp;&amp; max=$x; echo $max<\/strong>\r\n5476168<\/pre>\n<p>You may be wondering why I don&#8217;t simply use the ability of awk to perform calculations. Well that is certainly possible, but I may not always be using awk to produce the numbers in the first place, and this is supposed to be a generic recipe.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You have a a column of numbers that you have produced in some manner such as :- $ awk &#8216;\/clean message\/ {print $(NF-1)}&#8217; mail.info.log &#8230; 100935 12197 3606 84653 4498 99110 4762 3001 10889 12611 12249 12245 136599 49097 6668 And you want a quick and dirty way of finding the largest number. Well there\u00a0is <a href='https:\/\/really.zonky.org\/?p=3967' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false,"_share_on_mastodon":"0"},"categories":[4,209,226],"tags":[43,1376,649,1212],"class_list":["post-3967","post","type-post","status-publish","format-standard","hentry","category-it","category-linux-it","category-working-notes","tag-linux","tag-maximum","tag-unix","tag-zsh","category-4-id","category-209-id","category-226-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-11Z","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3967"}],"version-history":[{"count":4,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3967\/revisions"}],"predecessor-version":[{"id":3985,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3967\/revisions\/3985"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}