Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Created January 26, 2023 07:05
Show Gist options
  • Save oguz-ismail/4326db1370e25a84eed02daa97a2a3c2 to your computer and use it in GitHub Desktop.
Save oguz-ismail/4326db1370e25a84eed02daa97a2a3c2 to your computer and use it in GitHub Desktop.
# sample input: 10-1000,15-350,50-1500,2100,1700-1800,45,40,145,2-1300
set -o pipefail
tr , '\n' | sort -n | awk '
BEGIN {
FS = OFS = "-"
}
NF < 1 || NF > 2 || (NF == 2 && $1 > $2) {
print "bad input: " $0 | "cat >&2"
exit 1
}
NF == 1 {
$2 = $1
}
$2 <= end {
next
}
$1 <= end {
end = $2
next
}
{
emit()
start = $1
end = $2
}
END {
emit()
}
function emit() {
if (NR != 1) {
if (start == end)
print start
else
print start, end
}
}' | paste -sd,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment