shell / awk question
Roger Rohrbach
roger at wrs.com
Sat Oct 20 10:48:59 AEST 1990
emwessel at praxis.cs.ruu.nl (Eric van Wessel) writes:
> quota | awk 'BEGIN { FS=" " } { quot=$2 }
> END { printf "%d\n",quot } '
> ^^
> How to make quot known here ?
> |
> |
> |
> |
> du -s $HOME | |
> awk 'BEGIN { FS=" " } { printf "\nDiskspace\n"; |
> printf "---------\n"; |
> used=($1)/2; |
> free=quot-used } <----------------
> END { printf "Used : %d kb. (%d%%)\n",used,used/(quot/100) ;
> if (free>=0)
> printf "Free : %d kb.\n\n",free
> else printf "Too much : %d kb.\n\n",-free } '
(I'm following up as well as replying by mail because I think this
is of general interest and obscure enough to be educational.)
A general method for integrating two awk programs that read different
data streams is to identify unique characteristics of the two streams,
and then to read them both at once, guarding the actions in the single
awk program with conditions that ensure that the appropriate rules for
each stream are executed only for input records read therefrom.
For instance, in the example above, the guard for the output of the
"quota" output stream might be
$2 ~ /^[1-9][0-9]*$/
and that for the "du -s" output stream might be
index($2, "/" == 1)
so that the following (Bourne) shell program gives the desired result:
{
quota
du -s $HOME
} | awk '
$2 ~ /^[1-9][0-9]*$/ {
quot=$2
}
index($2, "/" == 1) {
printf "\nDiskspace\n";
printf "---------\n";
used=($1)/2;
free=quot-used
}
END {
printf "%d\n", quot;
printf "Used : %d kb. (%d%%)\n", used, used/(quot/100);
if (free >= 0)
printf "Free : %d kb.\n\n", free
else
printf "Too much : %d kb.\n\n", -free
}
'
--
Roger Rohrbach sun!wrs!roger roger at wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
| {o >o |
| \<>) "If I were a gas, I'd be inert!" |
More information about the Comp.unix.shell
mailing list