The program reads a sequence of integers (sorted), and outputs x-y
ranges for consecutive integers, outputting it verbatim otherwise.
cat | python ranges.py
1
2
3
7
8
9
^D
1-3
7-9
ranges
is actually a DFA/FSM!
Use case: for a NTFS partition, show which files are in bad sectors marked by ddrescue.
ddrescuelog
outputs individual blocks (sectors). On the other hand, the -s
option to ntfscluster
can accept x-y
ranges. By "factoring" the list of blocks into ranges, we can reduce the number of (slow) disk accesses.
ddrescuelog --domain-logfile=sdN.domain --list-blocks=- rescue.log \
| python ranges.py \
| xargs -n1 -i \
sudo ntfscluster -s \{} 2>/dev/null
(redirected stderr for ntfscluster
as it tends to output a lot of "Error reading inode NNN" lines)