Fill data if number range is given

Hi
I want to get all numbers if number range is given as input.
Eg:

INPUT FILE

100-105
107
108-112

OUTPUT REQUIRED:

100 101 102 103 104 105
107
108 109 110 111 112

How can I do it using shell? :confused:

Thanks in advance.

Please use code tag

Try

$ cat file
100-105
107
108-112
$  awk  -F'[-]' '{for(i=$1;i<=c=(NF==1)?$1:$2;i++) printf i OFS ;printf RS}' file

100 101 102 103 104 105 
107
108 109 110 111 112
1 Like
awk -F"-" '/^[0-9]/ { print "seq ",$1,(NF==1)?$1:$2,"|paste -s";next} {print "echo ",$0;}' filename | sh

Thank You Akshay & Pravin.
Now it is working beautifully.

Hi Guys,

In the same case, a little different scenario. If the requirement is like :

Input File:

HELLO 100-105 107 108-112
HI 201 204-209 210

OUTPUT Required:

HELLO~100 HELLO~101 HELLO~102 HELLO~103 HELLO~104 HELLO~105 HELLO~107 HELLO~108 HELLO~109 HELLO~110 HELLO~111 HELLO~112
HI~201 HI~204 HI~205 HI~206 HI~207 HI~208 HI~209 HI~210

i.e. the first field is prefix to each number.

Thanks in advance.

And where are the code tags? With 68 post you should know how to do it.

Added code tag !!!

This needs gnu awk , or awk supporting more than on character in RS

awk -F- '/^[[:alpha:]]/ {s=$0;print"";next} /^[[:digit:]]+-[[:digit:]]+/ {for (i=$1;i<=$2;i++) printf "%s~%s ",s,i;next} {printf "%s~%s ",s,$1} ' RS=" |\n" file

HELLO~100 HELLO~101 HELLO~102 HELLO~103 HELLO~104 HELLO~105 HELLO~107 HELLO~108 HELLO~109 HELLO~110 HELLO~111 HELLO~112
HI~201 HI~204 HI~205 HI~206 HI~207 HI~208 HI~209 HI~210

EDIT: some more readable:

awk -F- '
/^[[:alpha:]]/ {
	s=$0
	print""
	next
	}
/^[[:digit:]]+-[[:digit:]]+/ {
	for (i=$1;i<=$2;i++)
		printf "%s~%s ",s,i
	next
	}	
/^[[:digit:]]+$/ {
	printf "%s~%s ",s,$1
	} 
' RS=" |\n" file

Try this

$ awk  '{for(i=2;i<=NF;i++){split($i,A,"-");for(j=A[1];j<=c=(A[2]>A[1])?A[2]:A[1];j++)printf $1"~"j OFS}printf RS}' file 
HELLO~100 HELLO~101 HELLO~102 HELLO~103 HELLO~104 HELLO~105 HELLO~107 HELLO~108 HELLO~109 HELLO~110 HELLO~111 HELLO~112 
HI~201 HI~204 HI~205 HI~206 HI~207 HI~208 HI~209 HI~210 
1 Like

Amazing superb .
Worked beautifully :slight_smile:
Thanks Akshay.

@Jotne: Thanks for the prompt reply.

Following may also help for same.

awk -F"-" '{a=$1; b=$2;} b{for(i=0;i<=NR;i++) {for(;a<=b;a++) print a" ";} print"\n"} !b {print a"\n"}' ORS="" file_name

Output will be as follows.

100 101 102 103 104 105
107
108 109 110 111 112

Thanks,
R. Singh

Ravinder you can simplify your code
one for loop also you can remove,so that processing will be faster, since loop for(i=0;i<=NR;i++) does not have effect except time consuming, because after 1st iteration a will be equal to b that is { for(;a<=b;a++) print a" " } . It will simply loop till current NR , look at the following

$ cat file
100-105
107
108-112
$ awk '{print "NR--> "NR,$0 OFS}END{print "\n"}' ORS="" OFS="\t" file
NR--> 1    100-105    NR--> 2    107    NR--> 3    108-112    
$ awk -F"-" '{a=$1; b=$2;} b{for(i=0;i<=NR;i++) {print  "looping--> "i" ";for(;a<=b;a++) print a" ";} print"\n"} !b {print  a"\n"}' ORS="" file
looping--> 0 100 101 102 103 104 105 looping--> 1 
107
looping--> 0 108 109 110 111 112 looping--> 1 looping--> 2 looping--> 3
$ awk -F"-" '{a=$1; b=$2;} b{for(;a<=b;a++) print a" ";print"\n"} !b {print a"\n"}' ORS="" file
100 101 102 103 104 105 
107
108 109 110 111 112