Path: utzoo!utgpu!attcan!uunet!mcvax!cernvax!ethz!solaris!wyle From: wyle@solaris.UUCP (Mitchell Wyle) Newsgroups: comp.unix.wizards Subject: number range parsing in sh(1) Keywords: range, 1,2,4-9,18- Message-ID: <469@solaris.UUCP> Date: 26 Jul 88 09:20:31 GMT Organization: SOT Sun Cluster, Swiss Federal Institute of Tech. Lines: 49 Is there a better way to parse numeric ranges than the following? ------------------------- snip here ------------------------- #!/bin/sh # # parse a range of numbers in the form: # # :== # | ,range # | - # | - # # :== # # :== 0|1|2|3|4|5|6|7|8|9 # # ...to create a list of numbers; e.g. 1,3,8-11 -> 1 3 8 9 10 11 # # input range is in variable $RA ($1 for this example) RA=$1 echo in: $RA R="" RA=`echo $RA | sed 's/,/\ /g'` for tok in $RA ; do if echo $tok | grep -s '\-' ; then t=`echo $tok | sed 's/-/ - /'` set $t i=$1 max=${3:-99} while test $i -le $max ; do R=$R" "$i i=`expr $i + 1` done else R=$R" "$tok fi RA=$R done echo out: $RA ------------------------- snip here ------------------------- Is there a faster way? How does Larry Wall's mailpatch work? Curious minds want to know! -- -Mitchell F. Wyle wyle@ethz.uucp Institut fuer Informatik wyle%ifi.ethz.ch@relay.cs.net ETH Zentrum 8092 Zuerich, Switzerland +41 1 256-5237