Re: awk problem converting datafile





Michael Tosch wrote:
<snip>
nawk '
/ CET / { if (cet++>0) print dat,sum; dat=$3"-"$2"-"$6; sum=0; next }
/'"$1"'/ { sum+=$2 }
END { if (cet>0) print dat,sum }
' datafile

}

You might want to chage the way you're passing the shell variables value to awk to avoid obscure failures for some settings of "$1". This would be more robust:


nawk -v sel="$1" '
/ CET / { if (cet++>0) print dat,sum; dat=$3"-"$2"-"$6; sum=0; next }
$0 ~ sel { sum+=$2 }
END { if (cet>0) print dat,sum }
' datafile

}

See question 24 in the FAQ (http://home.comcast.net/~j.p.h/cus-faq-2.html#24) for details.

	Ed.
.



Relevant Pages