Path: utzoo!utgpu!attcan!uunet!pilchuck!ssc!mcgp1!fst From: fst@mcgp1.UUCP (Skip Tavakkolian) Newsgroups: comp.unix.wizards Subject: Re: summary: number range parsing in sh(1) Summary: awk is faster than sh on this one. Keywords: IFS, /bin/sh, parse Message-ID: <1492@mcgp1.UUCP> Date: 3 Aug 88 08:56:23 GMT References: <470@solaris.UUCP> Organization: Computer Tools Int'l Inc. Lines: 89 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 > # to create a list of numbers to send such as 1,3,8-11 -> 1 3 8 9 10 11 > RA=$1 > R="" > echo in: $RA > SIFS="$IFS" > IFS="," > for i in $RA ; do > R=$R" "$i > done > RA=$R > IFS=$SIFS > R="" > for tok in $RA ; do > case $tok in > *-*) > SIFS="$IFS" > IFS='-' > set $tok > IFS="$SIFS" > i=$1 > max=${2:-99} > while test $i -le $max ; do > R=$R" "$i > i=`expr $i + 1` > done ;; > *) > R=$R" "$tok ;; > esac > RA=$R > done > echo out: $RA > -- > -Mitchell F. Wyle wyle@ethz.uucp Here is an AWK script that does what you want and some time measurements as run on a 3b2-600 SYSV 3.1.1 using time(1) (I am using the old awk). awk ' END { # no error checking done here arg = "'$1'" if (length(arg) == 0) exit print "in: ", arg printf "out: " NF = split(arg, args, ",") for (i = 1 ; i <= NF ; i++) { if (split(args[i], range, "-") < 2) printf "%d ", args[i] else { for (j = range[1]; j <= range[2]; j++) printf "%d ", j } } print }'