How to generate a series of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to generate a series of numbers
# 1  
Old 04-04-2008
How to generate a series of numbers

Hi All,

I have a requirement where in I have an input as follows:-
input=1-4,6,8-10,12-15

I need to explode this range into an output file as follows:-
1
2
3
4
6
8
9
10
12
13
14
15

My input may vary like 1,5-9,11-13,15-17.......
So depending on the input the output file should have the required number of records.

How can this be done? Please help.

Thanks & Regards,
Rony
# 2  
Old 04-04-2008
Use awk.
Code:
input='1-4,6,8-10,12-15'
echo $input | awk -F, '{for(i=1;i<=NF;i++){n=split($i,a,"-");for(j=a[1];j<=a[n];j++)print j}}'


Last edited by danmero; 04-04-2008 at 12:46 AM..
# 3  
Old 04-04-2008
Thanks you very much danmero Smilie
It works perfect for all the scenarios like 1-4,6... or 1,4-6,....
Now I need analyse what your code is doing.
# 4  
Old 04-04-2008
With zsh:

Code:
setopt braceccl
eval print -l $(printf "{%s}\n" $=input:gs/-/../:gs/,/\ )

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving decimal point in a series of numbers

Hi, i need to move the decimal point from a file listing some numbers like this : 49899.50 49914.55 49894.48 49939.65 49879.44 49919.57 49934.62 49944.67 49954.72 (1 Reply)
Discussion started by: Board27
1 Replies

2. Shell Programming and Scripting

Auto generate Line Numbers

How do I generate line numbers in Vi? I have this: ,'04-90020-039N','61423','2GDV00039-0002', SYSDATE); ,'04-90020-040D','61423','2GDV00046-0001', SYSDATE); ,'04-90020-041N','61423','2GDV00038-0002', SYSDATE); ,'04-90020-043D','61423','2GDV00047-0001', SYSDATE);... (3 Replies)
Discussion started by: djehresmann
3 Replies

3. Shell Programming and Scripting

AWK series of numbers

Hi, I have a dataset say 1 2 3 4 5 5 6 7 6 7 8 9 I was wondering if there is a way to add another column with the following style... 1 2 3 4 xyz_1 5 5 6 7 xyz_2 6 7 8 9 xyz_3 It would be greatly appreciated if I can have an option of specifying what to write instead of xyz,... (8 Replies)
Discussion started by: jacobs.smith
8 Replies

4. Shell Programming and Scripting

How to generate 10.000 unique numbers?

hello, does anybody can give me a hint on how to generate a lot of numbers which are not identically via scripting etc? (7 Replies)
Discussion started by: xrays
7 Replies

5. UNIX for Dummies Questions & Answers

To find missing numbers from a number series

Hi, My requirement is I have an input file with a continuous series from 10000 to 99999. I have some numbers missing from those series. I want a output file which produces those missing numbers. Eg: 10002, 99999 are missing from the series then the output file should contain those... (4 Replies)
Discussion started by: rakeshbharadwaj
4 Replies

6. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

7. Shell Programming and Scripting

How to code a series of numbers in SH

Hi. How can I write this in a shorter way? for NUMs in 1 2 3 4 5 6 I looked at the"seq" command, but I do not think that is what I want. Can I code for NUMs (1..7) Hey!! Is that it? (8 Replies)
Discussion started by: Ccccc
8 Replies

8. Shell Programming and Scripting

Shell script to generate Fibonacci series using recursion

I am facing problem with Shell script to generate Fibonacci series using recursion i.e. recursive function. Here is my script: #!/bin/sh fibo() { no=$1 if ; then return 0 elif ; then return 1 else a1=`expr $no - 1` fibo $a1 ... (10 Replies)
Discussion started by: Tapas Bose
10 Replies

9. Shell Programming and Scripting

How to sort out the latest one from a series of numbers?

Hi, I have a directory which contains a number of sub directories. They are named as 1.0.0, 1.0.1, 1.0.2...1.1.0..1.1.1...1.2.0..and so on.. Basically these are the tags created at the time of release. Tags are named as major.minor.buildnumber format for modules. Now I have to search the... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

10. Shell Programming and Scripting

generate level numbers

Hi... I have a sequence of jobs and its predecessors.. Input Job_Name Predecessor A NULL B1 A B2 A B3 B1 C B3 C B2 So based on these i have to generate the level Number What i mean is Let A be level 1 for B1 to happen it should have done A so B1 level is A+1 = 1+1 = 2 (12 Replies)
Discussion started by: pbsrinivas
12 Replies
Login or Register to Ask a Question