0

So I have a data file with lines like this:

class subject
1-2   x
3     y
4-5   a

I need to extract the ranges given in the class column and print out the text in subject as many times as the ranges says so. I should get something like this

class subject
1     x
2     x
3     y
4     a
5     a

αғsнιη
  • 41,407
SPL
  • 13
  • I wanted to extract the ranges' numbers to two separate variables then substract them from each other. But I have trouble separating the value returned by awk. I need an other instance to do it? – SPL Dec 02 '20 at 10:34
  • Sorry, that is a typo – SPL Dec 02 '20 at 10:34

1 Answers1

3

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
αғsнιη
  • 41,407