Using awk
:
awk '{ if ( split($1, range,"-")>1 ){
for(i=range[1]; i<=range[2]; i++) { print i, $2; };
next;
};
}1' infile
split()
the first column on -
character and use first and second index as start and end of the range within loop and print the same line with incremental on range everytime, then read next
line and do the same; if there was no range then output line with awk default print action 1
.
if there are other characters than dash for range, you can modify the third argument of the split()
function to mention that too, like:
split($1, range, /-|,|\.\./)
to work on -
, ,
and ..
as range characters.
sample input (Tab delimited):
class subject
1,2 x
3 y
4-5 a
3..6 z
Output with modified split()
to recognize -
, ,
and ..
as range separators:
class subject
1 x
2 x
3 y
4 a
5 a
3 z
4 z
5 z
6 z