Skip to content

Instantly share code, notes, and snippets.

@orymate
Created July 25, 2011 12:59
Show Gist options
  • Save orymate/1104063 to your computer and use it in GitHub Desktop.
Save orymate/1104063 to your computer and use it in GitHub Desktop.
Print set of numbers as ranges
#!/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