Skip to content

Instantly share code, notes, and snippets.

@okhlybov
Created April 18, 2020 19:14
Show Gist options
  • Save okhlybov/ed3a6e291c10bf34deb0f2796dd4ff5d to your computer and use it in GitHub Desktop.
Save okhlybov/ed3a6e291c10bf34deb0f2796dd4ff5d to your computer and use it in GitHub Desktop.
Ruby shell filter to sort space/newline delimited strings based on the floating point values extracted from the strings
#!/usr/bin/env ruby
# Scan space or newline delimited string list read from standard input for
# floating point values and sort it based on to the numbers found
def s2fp(s) ((s =~ /[-+]?[0-9]*\.?[0-9]+(e[-+]?[0-9]+)?/i).nil? ? 0 : $&).to_f end
puts ARGF.readlines.collect{|s| s.split}.flatten.sort{|a,b| s2fp(a) <=> s2fp(b)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment