summary: number range parsing in sh(1)

WU SHI-KUEI wu at spot.Colorado.EDU
Wed Aug 3 08:10:04 AEST 1988


In article <470 at solaris.UUCP> wyle at 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> :== <number>
#         |   <number>,range
#         |   <number>-<number>
#         |   <number>-
#
# <number>:== <digit><number>
#
# <digit> :== 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



More information about the Comp.unix.wizards mailing list