Created
July 25, 2011 12:59
-
-
Save orymate/1104063 to your computer and use it in GitHub Desktop.
Print set of numbers as ranges
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
#!/bin/sh | |
sort -n | awk ' | |
BEGIN { last = "x" } | |
/^[0-9]+$/ { | |
if (last == "x") { | |
range = $1 | |
} | |
else if (last + 1 != int($1)) { | |
if (range != last){ | |
printf "%d-%d\n", range, last | |
range = int($1) | |
} | |
else { | |
printf "%d\n", last | |
range = int($1) | |
} | |
} | |
last = $1 | |
} | |
END { | |
if (range != last){ | |
printf "%d-%d\n", range, last | |
} | |
else { | |
printf "%d\n", last | |
} | |
}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment