Skip to content

Instantly share code, notes, and snippets.

@andrewagain
Created January 28, 2015 01:52
Show Gist options
  • Save andrewagain/40217c6425a7abbbd375 to your computer and use it in GitHub Desktop.
Save andrewagain/40217c6425a7abbbd375 to your computer and use it in GitHub Desktop.
Sort lines, ignoring leading and trailing blank lines
#!/usr/bin/ruby
input = STDIN.read
lead_regex = /\A[\n\s]*\n/
tail_regex = /[\n\s]*\Z/
lead_blank = input[lead_regex]
tail_blank = input[tail_regex]
content = input.sub(lead_regex, "").sub(tail_regex, "")
sorted = content.lines.collect { |l| l.sub(/\n\Z/, "") }.sort.join("\n")
finished = "#{lead_blank}#{sorted}#{tail_blank}"
STDOUT.write finished
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment