My question is, given a list of integers, is it possible in bash to a) find all the sequences of consecutive numbers, then b) remove all but the last numbers in those sequences?
For example, given this list and assuming that the numbers are stored, one per line, in a .txt file,
001
002
003
005
007
010
011
012
is there a program/set of programs that would produce output
003
005
007
012
and if so, how? Thank you for your time.
EDIT:
Here's what I have so far:
#!/bin/bash
cat file.txt | numinterval >> interval.txt
integer=''
while read -u 3 interval
do
if [[ "$interval" -ne "1" ]]
then echo "$integer" >> desequenced.txt
else read -u 4 integer
fi
done 3< interval.txt 4< file.txt
The central idea is to run the sorted list of integers through numinterval, then to check if the numinterval list has any ones. If it does, move on to the next integer. If not, print the corresponding integer to a file.
10508
10861
10862
10906
10906
10909
10909
10950
10950
11179
11181
11182
11325
11325
11341
11341
11428
11428
Here is the output. Obviously something has gone wrong, as not only are consecutives not removed, there is a huge amount of whitespace after the list has ended.
Any help is appreciated.