summary: number range parsing in sh(1)

Michael Morrell morrell at hpsal2.HP.COM
Tue Aug 2 16:19:08 AEST 1988


/ hpsal2:comp.unix.wizards / wyle at solaris.UUCP (Mitchell Wyle) / 12:03 am  Jul 28, 1988 /

Here is the code now.  Anyone care to make it even
faster?  >> grin <<   perl?  awk?   ;-)

[ code deleted ]
-- 
-Mitchell F. Wyle            wyle at ethz.uucp
Institut fuer Informatik     wyle%ifi.ethz.ch at relay.cs.net
ETH Zentrum                  
8092 Zuerich, Switzerland    +41 1 256-5237
----------
Well, this is an awk version of this which I wrote some time back.
Note it also handles something of the form "-8" as a shorthand for "1-8".
This seemed to run significantly faster on my machine that any of the
posted shell scripts.

   Michael Morrell
   
-------------------
#! /bin/sh
RA=$1
echo in: $RA
RA=`echo $RA | awk -F, -f parse.awk`
echo out: $RA
-------------------

where parse.awk contains
-------------------
{ for (i = 1; i <= NF; i++) {
    j = index($i,"-");
    if (j == 0)
      printf "%d ",$i
    else {
      if (j == 1)
        kmin = 1;
      else
        kmin = substr($i,1,j-1) + 0;

      if (j == length($i))
        kmax = 99;
      else
        kmax = substr($i,j+1) + 0;

      for (k = kmin; k <= kmax; k++)
        printf "%d ",k
    }
  }
}
-------------------



More information about the Comp.unix.wizards mailing list