Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!ames!ncar!boulder!spot!wu From: wu@spot.Colorado.EDU (WU SHI-KUEI) Newsgroups: comp.unix.wizards Subject: Re: summary: number range parsing in sh(1) Message-ID: <2469@boulder.Colorado.EDU> Date: 2 Aug 88 22:10:04 GMT References: <470@solaris.UUCP> <14670001@hpsal2.HP.COM> Sender: news@boulder.Colorado.EDU Reply-To: wu@spot.Colorado.EDU (WU SHI-KUEI) Organization: University of Colorado, Boulder Lines: 48 In article <470@solaris.UUCP> wyle@solaris.UUCP (Mitchell Wyle) writes: >... Here is the code now. Anyone care to make it even >faster? >> grin << perl? awk? ;-) #! /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 to send such as 1,3,8-11 -> 1 3 8 9 10 11 # # input range is in variable $RA ($1 for testing) Using a severe test like 1,2,3-8,11-15,34,35,127-199 for $1, the following awk program runs about 20 times faster on a 3B2/400 than Chris Torek's solution. ---------------------- cut here -------------------------------- echo "$1" | awk -F',' ' { for (i = 1; i <= NF; ++i) { if ((p = index($i, "-")) == 0) printf"%s ", $i else { start = substr($i, 0, p - 1) end = substr($i, p + 1) for (j = int(start); j <= int(end); ++j) printf"%s ", j } } printf"\n" }' ------------------------------------------------------------------ Just a guest on this machine. In real life Carl Brandauer daemon associates 1760 Sunset Blvd Boulder, CO 80302 303-442-1731 uunet!nbires!bdaemon!carl