requires gawk 3.1+
function dirlist(dir,pat, array1,x) {
if (!pat || !dir) {
perror("No pattern or directory for file matching.", 1)
}
command = "ls -l " dir pat " | sort"
while ((command |& getline) > 0) {
array1[$9] = $5 / 1024
}
parray(array1,x)
tlist = array_to_list(array1)
return tlist
}
function array_to_list(arrname, tlist,m) {
for (m in arrname) {
tlist = length(tlist) < 1 ? arrname[m] : tlist" "arrname[m]
}
return tlist
}
function perror(msg,flg) {
printf "%s\n", msg
if (flg) {
exit
}
return
}
function parray(arrname,len, x) {
while (x <= len) {
print arrname[x++]
}
return
}
BEGIN {
x = 0;
xlist = dirlist("/home/me/","*.txt"

if (xlist) {
split(xlist,arr)
for (x in arr) {
print x , arr[x]
}
}
}
{
more stuff
}
It's a lot easier in the shell.