红宝石答案
也许其他人可以为您提供您要求的 Bash 或 Awk 解决方案。但是,我认为任何基于 shell 的答案都可能针对您的数据集非常本地化,并且不是很可扩展。用 Ruby 解决问题相当简单,并且为您提供了灵活的格式设置和更多选项,用于在以后以其他方式操作数据集。YMMV。
#!/usr/bin/env ruby
# You could read from a file if you prefer,
# but this is your provided corpus.
nums = [5, 6, 7, 8, 15, 16, 17, 25, 26, 27]
# Find gaps between zero and first digit.
nums.unshift 0
# Create array of arrays containing missing digits.
missing_nums = nums.each_cons(2).map do |array|
(array.first.succ...array.last).to_a unless
array.first.succ == array.last
end.compact
# => [[1, 2, 3, 4], [9, 10, 11, 12, 13, 14], [18, 19, 20, 21, 22, 23, 24]]
# Format the results any way you want.
puts missing_nums.map { |ary| "#{ary.first}-#{ary.last}" }
鉴于您当前的语料库,这会在标准输出上产生以下内容:
1-4
9-14
18-24