Create range from a list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create range from a list
# 1  
Old 05-14-2010
Create range from a list

Hello everyone,

I am trying to create a script that will make a range or ranges based on a sorted list of numbers.

Eg. If the list is like
Code:
1
2
3
4
5
6
7
12
13
14
15

The output range should be:
Code:
1-7
12-15

Would be greateful if you could point me in the right direction,

Cheers,
rte

Last edited by radoulov; 05-14-2010 at 11:21 AM.. Reason: Please use code tags!
# 2  
Old 05-14-2010
Code:
awk 'END {
  if (prev != max)
    print min, "-", prev
  }   
NR == 1 { min = $1 }  
$1 != prev + 1 {
  if (prev)
    print min, "-", prev
  min = $1; 
  }
{ prev = $1 }' infile


Last edited by radoulov; 05-14-2010 at 11:36 AM.. Reason: Modified ...
# 3  
Old 05-14-2010
Does't work for me Smilie


I have created a file called test with the following content
1
2
3
4
5
10
11
12

but when I try to run

Code:
awk 'END {
  if (prev != max)
    print min, "-", prev
  }
$1 != prev + 1 {
  print min, "-", prev
  min = $1 
  }
{ 
  prev = $1 
  NR == 1 && min = $1 
  }' test

I get the error:
Code:
awk: syntax error near line 4
awk: bailing out near line 4

What am I doing wrong Smilie
# 4  
Old 05-14-2010
Use nawk or /usr/xpg4/bin/awk on Solaris.
This User Gave Thanks to radoulov For This Post:
# 5  
Old 05-14-2010
Thanks radoulov,

That works perfectly !! Smilie
# 6  
Old 05-14-2010
Another approach:
Code:
awk '!r{r=n=$1;next}
++n!=$1{print r "-" --n;r=n=$1}
END{print r "-" n}
' file

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 7  
Old 05-14-2010
Wasn't something similar answered here?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

TCL script to capture range of lines and create two independent variables

Hi I am having a code as stated below module abcd( a , b , c ,da , fa, na , ta , ma , ra , ta, la , pa ); input a , b, da ,fa , na , ta , ma; output c , ra ,ta , la ,pa ; wire a , b , da , fa ,na , ta , ma; // MBIST Structures... (1 Reply)
Discussion started by: kshitij
1 Replies

2. Shell Programming and Scripting

Convert list of numbers to text range

Hi, I'd like to take a list of numbers (with a prefix) and convert to a range, for example: cn001 cn004 cn016 cn017 cn018 cn019 cn020 cn021 cn031 cn032 cn038 cn042 cn043 cn044 cn045 (5 Replies)
Discussion started by: chrissycc
5 Replies

3. Shell Programming and Scripting

How to create individual entries from a range of numbers?

I want to create entries based on the series as in examples below: Input: 2dat3 grht-5&&-15 3dat3 grht-16&&-30 4dat3 ftht-4&&-12 5sat3 ftht-16&&-20 Output: 2dat3 grht-5 2dat3 grht-6 2dat3 grht-7 2dat3 grht-8 (7 Replies)
Discussion started by: aydj
7 Replies

4. Shell Programming and Scripting

For Loop Range Create Directories

Hello, I am a bit stumped on this. I am attempting to create 24 empty directories with a loop. Seems like I have incorrect syntax. When I run the following command I get the error below. Command $ for i in {2..24}; do mkdir $i_MAY_2011 ; doneError x 24 mkdir: missing operand Try `mkdir... (2 Replies)
Discussion started by: jaysunn
2 Replies

5. Shell Programming and Scripting

Using 'date' to list a range of dates

Hi guys, I have been trying to create a list of dates from a certain range, ie. range from 01011950 to 31122000 But when my below code reaches certain dates, it comes up with a; 'date: invalid date 'yyyy-mm-dd -d 1day' Sofar I have come up with the following, slow and ugly; ... (4 Replies)
Discussion started by: TAPE
4 Replies

6. Shell Programming and Scripting

List/Range Todays Log.

Hi All, I am mediator Shell programmer, Just have an hands on experice :-), i am writing a shell scirpt to list logs of todays date from /var/log/messages. I need to ur kind help where if i run this script from cron. the script should filter todays logs only from /var/log/messages. Below... (4 Replies)
Discussion started by: anand.kulkarni
4 Replies

7. Shell Programming and Scripting

To Create range of values

Hi, I have a file with the below like values with integers only in sorted order (May or may not be in sequence) Eg: File1.txt ----------- 1 2 3 4 5 6 . . . . . 10000 My requirement here is to create a range of values out put to a temp k (4 Replies)
Discussion started by: shiva447
4 Replies

8. UNIX for Dummies Questions & Answers

List-to-Range of Numbers

Hello, I have two columns with data that look like this: Col1 Col2 ------ ----- a 1 a 2 a 3 a 4 a 7 a 8 a 9 a 10 a 11 b 6 b 7 b 8 b 9 b 14 (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

9. Shell Programming and Scripting

Creating a range out of a broken list

Hi all, I am trying to create a file which has one or more ranges based on a file containing a long list. The problem is that the file which has this list is not continuous and is broken in many places. I will try to illustrate by an example: The List File: 1 2 3 4 5 6 9 10 11 12... (5 Replies)
Discussion started by: run_time_error
5 Replies

10. Shell Programming and Scripting

to get list of files of a perticular range of time

hi, how to list the files which has been created or accessed before 6 months thanks (1 Reply)
Discussion started by: useless79
1 Replies
Login or Register to Ask a Question