如何计算文件编号并保存连续订单?

时间:2018-11-28 12:01:28

标签: bash range

假设我有一个像这样的文件

1
2
3
5
8
9

我想找出连续的订单:

consecutive order from 1 to 3
consecutive order from 5 to 6

1 个答案:

答案 0 :(得分:0)

让我们调用包含数据的文件 / tmp / dataFile ,您可以使用awk执行此操作,如下所示:

awk 'NR==1{lowerBound=$1;upperBound=$1;next} $1 == upperBound+1 {upperBound=$1;next} {print "consecutive order from " lowerBound,"to", upperBound;lowerBound=$1;upperBound=lowerBound} END{print "consecutive order from " lowerBound,"to",upperBound}' /tmp/dataFile