Small awk problem 
Author Message
 Small awk problem

Howdy, gang--

I've got a small problem I've not been able to figure out. I've read
the FAQ, and checked out all the books on my shelf, but I can't find
the answer. Perhaps someone here has it?


#!/bin/nawk -f

BEGIN {
   system( "df -k -F ufs" ) | getline

Quote:
}

# Only for lines starting with a / (i.e., ignore the heading)
/\// {
   print $1 " has " $4 "K available of " $2 "K"
   system( "df -k -F ufs" ) | getline

Quote:
}

When I run it, the script filters the output of df -k, as I'd like,
but then it wants to read from stdin. How do I make it stop?

/export/home/marco/awkDebug> ./t
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/dsk/c0t0d0s0     143909   64028   65491    50%    /
/dev/dsk/c0t0d0s6     481704  283013  150521    66%    /usr
/dev/dsk/c0t0d0s7    1088545   69306  986584     7%    /export/home
/dev/dsk/c0t0d0s5     143909  126998   11155    92%    /opt
/dev/dsk/c0t1d0s6    1973518 1216133  698180    64%    /share
/dev/dsk/c0t0d0p2     715850       4  694370     1%    /work
sh: 0: not found

I realize I could get the proper results (in this case) by:

#!/bin/sh

df -k -F ufs | awk '/\// { print $1 " has " $4 "K available of " $2
"K" }'

However, this isn't the job I want to do, it's simply a distilled-down
version of the problem so you can see what my problem is without
weeding through all my other code. What I'm ultimately trying to do is
use the standard awk pattern matching structures to read multiple
files in a programmatic way. However, this 'going to stdin' is really
getting to be a problem...

Thanks for any or all suggestions.
I'll be happy to summarize all replies. (I'll summarize on Monday).

--Marco



Fri, 21 Apr 2000 03:00:00 GMT  
 Small awk problem


Quote:
>Howdy, gang--

>I've got a small problem I've not been able to figure out. I've read
>the FAQ, and checked out all the books on my shelf, but I can't find
>the answer. Perhaps someone here has it?


>#!/bin/nawk -f

>BEGIN {
>   system( "df -k -F ufs" ) | getline
>}

># Only for lines starting with a / (i.e., ignore the heading)
>/\// {
>   print $1 " has " $4 "K available of " $2 "K"
>   system( "df -k -F ufs" ) | getline
>}

>When I run it, the script filters the output of df -k, as I'd like,
>but then it wants to read from stdin. How do I make it stop?

>/export/home/marco/awkDebug> ./t
>Filesystem            kbytes    used   avail capacity  Mounted on
>/dev/dsk/c0t0d0s0     143909   64028   65491    50%    /
>/dev/dsk/c0t0d0s6     481704  283013  150521    66%    /usr
>/dev/dsk/c0t0d0s7    1088545   69306  986584     7%    /export/home
>/dev/dsk/c0t0d0s5     143909  126998   11155    92%    /opt
>/dev/dsk/c0t1d0s6    1973518 1216133  698180    64%    /share
>/dev/dsk/c0t0d0p2     715850       4  694370     1%    /work
>sh: 0: not found

>I realize I could get the proper results (in this case) by:

>#!/bin/sh

>df -k -F ufs | awk '/\// { print $1 " has " $4 "K available of " $2
>"K" }'

>However, this isn't the job I want to do, it's simply a distilled-down
>version of the problem so you can see what my problem is without
>weeding through all my other code. What I'm ultimately trying to do is
>use the standard awk pattern matching structures to read multiple
>files in a programmatic way. However, this 'going to stdin' is really
>getting to be a problem...

>Thanks for any or all suggestions.
>I'll be happy to summarize all replies. (I'll summarize on Monday).

>--Marco

The following works:
BEGIN {
    while  ("df -k -F ufs" | getline) {
        # Only for lines starting with a / (i.e., ignore the heading)
        if ($0 ~ /^\//) {
            print $1 " has " $4 "K available of " $2 "K"
        }
    }

Quote:
}

      Gert


Sat, 22 Apr 2000 03:00:00 GMT  
 Small awk problem



% BEGIN {
%    system( "df -k -F ufs" ) | getline
% }

This is not correct. What it does is run the command "df...", then
try to execute the string returned by system (in this case, 0), and
pipe the output of _that_ command to getline. This is where the
"sh: 0: not found" in your sample output comes from.

If you want to start a pipeline from within awk, you have to
process the entire output of the command using getline, like
this:
 cmd="df -k -F ufs"
 while ((cmd | getline) > 0) {
  if ($1 ~ "^/") blah blah blah
 }
 close(cmd)

If you do all that in the BEGIN section, nawk won't try to read from
stdin.
--

Patrick TJ McPhee
East York  Canada



Mon, 24 Apr 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Q: Psion and Small (really small) talk

2. small small question about linking libraries

3. Small small question about linking libraries

4. small painting problem

5. A couple of small problems

6. Small problem with package dependencies

7. Small problem in porting 4.0 package to 5.1

8. COBOL Y2K problem is small

9. small hugs problem

10. Small problem with Greater Consequences...

11. A small problem requiring help

12. Problem with small GAWK programme

 

 
Powered by phpBB® Forum Software