Created
January 28, 2015 01:52
-
-
Save andrewagain/40217c6425a7abbbd375 to your computer and use it in GitHub Desktop.
Sort lines, ignoring leading and trailing blank lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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