🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.shell
Article #1555

Re: shell / awk question

#1555
From: Roger Rohrbach
Date: Sat, 20 Oct 1990 00:48
74 lines
2546 bytes
emwessel@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@wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \<>) "If I were a gas, I'd be inert!"                                    |
Technical Details
Message-ID: <1265@wrs.wrs.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!decwrl!pacbell.com!pacbell!rtech!wrs!roger
References: <4057@ruuinf.cs.ruu.nl>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!decwrl!pacbell.com!pacbell!rtech!wrs!roger
From: roger@wrs.com (Roger Rohrbach)
Newsgroups: comp.unix.shell
Subject: Re: shell / awk   question
Keywords: shell, awk
Message-ID: <1265@wrs.wrs.com>
Date: 20 Oct 90 00:48:59 GMT
References: <4057@ruuinf.cs.ruu.nl>
Sender: newsmgr@wrs.COM
Lines: 74
Nntp-Posting-Host: kymijoki