My question is the opposite of this one.
My input file is a list of locations and serial numbers (like an inventory list). Some of the serial numbers are listed as ranges (e.g., 11-17) and I need to convert each range to the full list of sequential numbers (e.g., 11, 12, 13, 14, 15, 16, 17).
The input format is like this:
Main Street # 12770-12786, 12980, 13012-13013, 13068, 13093, 13115, 13122, 13137-13156, 13548-13557, 13954-13969, 14471-14475, 14500-14508
Madison Ave # 14071-14074, 14105-14128, 14131-14140, 14603-14612
Each location is separated by an empty line. Each location starts with a name. So far I have only seen names containing [a-zA-Z -]
which is upper and lower case letters, spaces and dashes. The name starts at column 0 of a new row and it is followed by a space, a hash and a space: #
.
For each range in the format nnnn-mmmm
, I need to produce a comma (and space) separated list of the sequential values like n1, n2, n3, n4, n5
. For example, The inventory for Madison Ave (above) will need to be listed like this:
Madison Ave # 14071, 14072, 14073, 14074, 14105, 14106, 14107, etc.
Input is one text file and output can be one text file. I'd like to do the processing in bash, but I suppose I could use Python also.
I know some possible pieces of the solution, such as:
find the ranges with grep using a regex pattern like this:
grep -o -P '\d+-\d+'<input_file>
assume the first result of that is the range
4243-4263
echo {4243-4263} | sed 's/-/../'
use a for-loop on the above result like this:
for i in {4243..4263}; do echo $i; done
I don't know how to put all that together into a solution. I also assume there is probably a much better way to go about it.
e
flag to evaluate a perl expression within a regexs/pattern/replacement/
substitution – steeldriver Dec 15 '19 at 00:45