Create range from a list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create range from a list
# 8  
Old 05-14-2010
... and therefore it's most probably a homework.
# 9  
Old 05-14-2010
For the userland deviant who isn't satisfied with a simple, straighforward AWK solution Smilie:
Code:
echo '[dlb=Qdlb1+=MlPxlRx]sC [sbq]sM [lan[-]nlbpst]sP [q]sQ [dsasb]sR' | cat - data \
| sed '2,$s/-/_/; 2s/.*/& lRx/; 3,$s/.*/& lCx/; $s/.*/& lPx/' | dc

a = lower bound of sequence
b = upper bound of sequence
R = reset macro. Set a and b equal to the latest value read (this is the beginning of a new sequence).
M = max macro. Set the new upper bound of the sequence, b.
P = printing macro. Prints "a-b".
C = comparison macro. Determines if the newest value is part of an ongoing sequence. If it is, call the M (max) macro to store it in b. If it is not, call the P (print sequence) macro and then the R (reset) macro to begin a new sequence.
Q = quit macro

It handles negative values, sequences that span negative to positive (-1, 0, 1), and sequences which repeat the same value (1,2,3,3,3,4,5 => 1-5).

I'm a relative dc newbie, so if someone can do it better (with dc), I'd be interested in seeing your approach.

Test run:
Code:
$ cat data
-100
-99
-98
-97
-96
-4
-3
-2
-1
0
1
2
3
4
5
1
2
3
3
3
4
5
14
15
16
17
18
$
$ echo '[dlb=Qdlb1+=MlPxlRx]sC [sbq]sM [lan[-]nlbpst]sP [q]sQ [dsasb]sR' | cat - data \
> | sed '2,$s/-/_/; 2s/.*/& lRx/; 3,$s/.*/& lCx/; $s/.*/& lPx/' | dc
-100--96
-4-5
1-5
14-18

Regards,
Alister
# 10  
Old 05-14-2010
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $count=0;
my @arr=();

 my @data = <DATA>;

  foreach my $rec (@data) {
  $count++;
  chomp $rec;

  if ($rec == $data[-1]) {
    if ($rec-1 != $arr[-1]) {
    print "$arr[0]-$arr[-1]\n";
    print "$rec-$rec\n";
    exit;  }
    else {
    push @arr, $rec;
    print "$arr[0]-$arr[-1]\n";
    exit; }
    }

  if ($count == 1) {
  push @arr, $rec; }

  else {
  if ($rec-1 != $arr[-1]) {
  print "$arr[0]-$arr[-1]\n";
  @arr=();
  push @arr, $rec;
  $count=1; }
 
  else {
  push @arr, $rec; }
 }
}

__DATA__
1
2
3
4
5
6
7
12
13
14
15


Last edited by pseudocoder; 05-14-2010 at 10:27 PM.. Reason: optimized operators
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