Skip to content

Instantly share code, notes, and snippets.

@robmiller
Created February 13, 2018 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robmiller/c0f3d50dccfdd1101a65540ed1b39f2d to your computer and use it in GitHub Desktop.
Save robmiller/c0f3d50dccfdd1101a65540ed1b39f2d to your computer and use it in GitHub Desktop.
Sort a list of hostnames so that www. versions show up next to non-www. versions of the same domain
#!/usr/bin/env ruby
#
# Sometimes you want to sort a list of hostnames/domains and want
# example.com to sort next to www.example.com. This does that.
#
# Author: Rob Miller <r@robm.me.uk>
#
# Usage:
#
# $ cat foo.txt | sort-domains
#
# $ sort-domains foo.txt
#
# Assumes each line of input is a hostname.
ARGF.read.lines.sort_by { |u| u.sub(/^www\./, "") }.each { |u| puts u }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment