1# Written by Aleksey Cheusov <vle@gmx.net>, public domain 2# 3# This awk module is a part of RunAWK distribution, 4# http://sourceforge.net/projects/runawk 5# 6############################################################ 7 8# =head2 braceexpand.awk 9# 10# =over 2 11# 12# =item I<braceexp(STRING)> 13# 14# shell-like brace expansion. 15# 16# For example: 17# print braceexpand("ab{,22{,7,8}}z{8,9}") 18# -| abz8 abz9 ab22z8 ab22z9 ab227z8 ab227z9 ab228z8 ab228z9 19# 20# =back 21# 22 23#use "match_br.awk" 24 25BEGIN { 26 __runawk_expanded = 0 27} 28 29function __runawk_braceexpand1 (s, 30 31 left,mid,right,len,accu,i,ch,deep) 32{ 33 __runawk_expanded = 0 34 35 if (match_br(s, "{", "}")){ 36 left = substr(s, 1, RSTART-1) 37 mid = substr(s, RSTART+1, RLENGTH-2) "," 38 right = substr(s, RSTART+RLENGTH) 39 40 len = RLENGTH-1 41 42 s = "" 43 44 accu = "" 45 deep = 0 46 for (i=1; i <= len; ++i){ 47 ch = substr(mid, i, 1) 48 49 if (deep == 0 && ch == ","){ 50 if (s != ""){ 51 s = s " " 52 } 53 54 accu = __runawk_braceexpand1(accu) 55 56 if (gsub(/ /, ",", accu)){ 57 s = s __runawk_braceexpand1(left "{" accu "}" right) 58 }else{ 59 s = s left accu right 60 } 61 62 accu = "" 63 }else{ 64 accu = accu ch 65 66 if (ch == "{") 67 ++deep 68 else if (ch == "}") 69 --deep 70 } 71 } 72 73 __runawk_expanded = 1 74 } 75 76 return s 77} 78 79function braceexpand (s, arr,i,cnt,cont){ 80 cont = 1 81 82 while (cont){ 83 cont = 0 84 85 cnt = split(s, arr, / /) 86 s = "" 87 for (i=1; i <= cnt; ++i){ 88 if (i > 1) 89 s = s " " 90 91 s = s __runawk_braceexpand1(arr [i]) 92 cont = (cont || __runawk_expanded) 93 } 94 } 95 96 return s 97} 98