An AWK poser that's stumped a novice 
Author Message
 An AWK poser that's stumped a novice

Well, I am one trying to learn the AWK language a bit later in life,
but I have been given a poser by a friend of mine to try to work out.
The only clue he'll give me is that it involves the use of arrays and
I am still weak in arrays.

Given a script that runs a ps -ef command to get a sampling of
processes running on a Unix system, I'm to come up with an AWK script
that finds each unique process, counts them, and displays a table like
this:

User        Number of Processes

User1                 3
User2                16
User3                 9

This is the Unix script:

#! /bin/ksh
clear
(i=1
until [ "$i" -gt 15 ]
do
i=`expr $i + 1`
sleep 2
ps -ef > user.dat
done
break) &

So the output for this is a list that duplicates every so often (so
that it gets meaningful data).

Any thoughts?  

I also have to come up with a PERL script that provides the same
report.



Wed, 06 Oct 2004 09:03:02 GMT  
 An AWK poser that's stumped a novice
...

Quote:
>I also have to come up with a PERL script that provides the same
>report.

This is almost a 'help me do my homework' problem. Well, I'll help with the
last bit: run whatever awk script you come up with through a2p. Ain't I
helpful?!


Thu, 07 Oct 2004 00:26:10 GMT  
 An AWK poser that's stumped a novice

Quote:

> ...
> >I also have to come up with a PERL script that provides the same
> >report.

> This is almost a 'help me do my homework' problem.

Bzzzt.  Wrong.

Thanks for playing.

Quote:
> Well, I'll help with the last bit: run whatever awk script you come
> up with through a2p.

Never heard of it.

Quote:
> Ain't I helpful?!

Not at all.

I post in talk.origins, Harlan.  Some of THOSE fellows wrote the book on
"How to be a Pompous Ass."

Compared to them, you don't rate.

However, if that is what you're trying to do, by all means, give that
newsgroup a read for a few days.

You're a piker compared to them.



Thu, 07 Oct 2004 00:38:39 GMT  
 An AWK poser that's stumped a novice

% Given a script that runs a ps -ef command to get a sampling of
% processes running on a Unix system, I'm to come up with an AWK script
% that finds each unique process, counts them, and displays a table like
% this:
%
% User        Number of Processes
%
% User1                 3
% User2                16
% User3                 9

So, what have you tried? Keeping a count of processes per user is
pretty straight-forward: you just need to figure out which field
has the user name and then use that field as the index into an array
of counts.

Keeping track of whether you've seen a process already is a bit
more involved, but it essentially involves using the user name and
process id as indicies.

% ps -ef > user.dat

However this line over-writes user.dat each time, so it won't happen.

Anyway, take a stab at it, and many people will be happy to critique
your efforts.
--

Patrick TJ McPhee
East York  Canada



Thu, 07 Oct 2004 02:04:09 GMT  
 An AWK poser that's stumped a novice

Quote:


> % Given a script that runs a ps -ef command to get a sampling of
> % processes running on a Unix system, I'm to come up with an AWK script
> % that finds each unique process, counts them, and displays a table like
> % this:
> %
> % User        Number of Processes
> %
> % User1                 3
> % User2                16
> % User3                 9

> So, what have you tried? Keeping a count of processes per user is
> pretty straight-forward: you just need to figure out which field
> has the user name and then use that field as the index into an array
> of counts.

I haven't tried much yet because I have yet to envision how it would
find and discriminate each user.  But I'm a newbie.  Don't know why I
took this on at my age, but I did so want to get out of Windows and
try something challenging.

I understand Unix commands pretty well so far, but scripting is new to
me and it looks fun once I can get the hang of it.

The ps command runs every so often (I'll correct the error below - one
of transcribing, not one of the original script, which was not written
by me) and you might see some sort of header and then the users.  Then
it runs again and we see the same users running the same processes.  I
don't want it to double-count so I wondered if I shouldn't just cound
the process id instead of the user.

So I see something (simplified) like this

[Header]
User1      03    00:00   vi
User9      05    00:00   ed
User6      01    00:00   ksh
User1      02    00:00   ksh
User9      04    00:00   ksh
[Header]
(repeated info)

Quote:
> Keeping track of whether you've seen a process already is a bit
> more involved, but it essentially involves using the user name and
> process id as indicies.

Okay, so an index.  I read about that in the Aho, et al book!  

Quote:
> % ps -ef > user.dat

> However this line over-writes user.dat each time, so it won't happen.

You're right.  My mistake.  Should be ">>"

Quote:
> Anyway, take a stab at it, and many people will be happy to critique
> your efforts.

Fair enough.  Thank you.


Thu, 07 Oct 2004 06:59:02 GMT  
 An AWK poser that's stumped a novice
David Sienkiewicz

Quote:




>> % Given a script that runs a ps -ef command to get a sampling of
>> % processes running on a Unix system, I'm to come up with an AWK
>> script % that finds each unique process, counts them, and
>> displays a table like % this:
>> %
>> % User        Number of Processes
>> %
>> % User1                 3
>> % User2                16
>> % User3                 9

[..]

$ ps aux | sort | uniq -w 8 -c

Good luck

Michael Heiming
--
Remove the +SIGNS case mail bounces.



Thu, 07 Oct 2004 07:16:20 GMT  
 An AWK poser that's stumped a novice


...

Quote:
>$ ps aux | sort | uniq -w 8 -c

Unfortunately, that won't compile in comp.lang.awk...


Thu, 07 Oct 2004 07:20:47 GMT  
 An AWK poser that's stumped a novice
...
Quote:
>>$ ps aux | sort | uniq -w 8 -c

>Unfortunately, that won't compile in comp.lang.awk...

...

Tired joke. Ever tried to compile awk in awk?

So no one in this ng should ever propose nonscript alternatives using standard
POSIX tools that would perform the OP's task more efficiently?



Thu, 07 Oct 2004 14:26:11 GMT  
 An AWK poser that's stumped a novice

Quote:

> ...
>>$ ps aux | sort | uniq -w 8 -c

> Unfortunately, that won't compile in comp.lang.awk...

Fortunately, the OP wrote he would run UNIX, so he should have
sort/uniq available, he could pipe the output to awk and get very
easily, exactly what he wants. Left this part out, I'm sure he can
get it, on his own....;-)

Michael Heiming
--
Remove the +SIGNS case mail bounces.



Thu, 07 Oct 2004 16:23:55 GMT  
 An AWK poser that's stumped a novice


Quote:


>> ...
>>>$ ps aux | sort | uniq -w 8 -c

>> Unfortunately, that won't compile in comp.lang.awk...

>Fortunately, the OP wrote he would run UNIX, so he should have
>sort/uniq available, he could pipe the output to awk and get very
>easily, exactly what he wants. Left this part out, I'm sure he can
>get it, on his own....;-)

My point is that you seem to have mistaken comp.lang.awk for
comp.unix.shell.  They are not the same thing.


Thu, 07 Oct 2004 23:45:50 GMT  
 An AWK poser that's stumped a novice

Quote:





>>> ...
>>>>$ ps aux | sort | uniq -w 8 -c

>>> Unfortunately, that won't compile in comp.lang.awk...

>>Fortunately, the OP wrote he would run UNIX, so he should have
>>sort/uniq available, he could pipe the output to awk and get very
>>easily, exactly what he wants. Left this part out, I'm sure he can
>>get it, on his own....;-)

>My point is that you seem to have mistaken comp.lang.awk for
>comp.unix.shell.  They are not the same thing.

To paraphrase Henry Spencer: we shouldn't create beautiful new
impediments to understanding, just because our hammer is awk.

Posters recommend simple UNIX shell commands all the time here
when appropriate. Lurk before you post. ;^>

--

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada


    fake address                use address above to reply


                                                spam traps



Fri, 08 Oct 2004 00:42:46 GMT  
 An AWK poser that's stumped a novice


Quote:






>>>> ...
>>>>>$ ps aux | sort | uniq -w 8 -c

>>>> Unfortunately, that won't compile in comp.lang.awk...

>>>Fortunately, the OP wrote he would run UNIX, so he should have
>>>sort/uniq available, he could pipe the output to awk and get very
>>>easily, exactly what he wants. Left this part out, I'm sure he can
>>>get it, on his own....;-)

>>My point is that you seem to have mistaken comp.lang.awk for
>>comp.unix.shell.  They are not the same thing.

>To paraphrase Henry Spencer: we shouldn't create beautiful new
>impediments to understanding, just because our hammer is awk.

I disagree (as should be clear by now).  The general issue of the validity
of enforcing "on-topicness" is one of those "two kinds of people in the world"
things - neither of us is likely to change the other's mind on this.

Quote:
>Posters recommend simple UNIX shell commands all the time here
>when appropriate. Lurk before you post. ;^>

That's funny.
You might like to do a google search on that and get back to me...


Fri, 08 Oct 2004 01:34:44 GMT  
 An AWK poser that's stumped a novice

Quote:
> $ ps aux | sort | uniq -w 8 -c

Nice use of other programs and the shell, but even though the information
itself is there, it's still wrong.  The original poster specified the format
of the output.  All this does is change the problem from formatting ps
output, to formatting uniq output (that erroneously includes the header line
from ps).  (Besides, the OP is trying to learn awk, not the shell and
standard filters.)

But it does move the awk side to a place where the OP won't need to learn
about arrays (even though I thought that was his point).  This is closer:

echo "User        Number of Processes"; echo; ps aux | grep -v "^USER" |
sort | uniq -w 8 -c | awk "{printf \"%8-s%15d\n\",\$2,\$1}"

at least with my bash and ps on linux. (This should be one line)  On my
machine this yeilds:

User        Number of Processes

bin                   1
daemon                1
nobody                1
root                 37
xfs                   1

And, since I used awk, I'm back on-topic ;)

    - Dan


Quote:
> David Sienkiewicz





> >> % Given a script that runs a ps -ef command to get a sampling of
> >> % processes running on a Unix system, I'm to come up with an AWK
> >> script % that finds each unique process, counts them, and
> >> displays a table like % this:
> >> %
> >> % User        Number of Processes
> >> %
> >> % User1                 3
> >> % User2                16
> >> % User3                 9

> [..]

> $ ps aux | sort | uniq -w 8 -c

> Good luck

> Michael Heiming
> --
> Remove the +SIGNS case mail bounces.



Fri, 08 Oct 2004 01:35:27 GMT  
 An AWK poser that's stumped a novice
Oops...I don't see a real difference between "ps -ef" and "ps aux" for this
problem.  But I should have used the original poster's ps command, as well
as output format:
    echo "User        Number of Processes"; echo; \
    ps -ef | grep -v "^USER" | sort | uniq -w 8 -c | \
    awk "{printf \"%8-s%15d\n\",\$2,\$1}"
(using bash's command continuation character \)...Same results.

Of course, to really be correct, we would let the OP's script [elsewhere in
thread] build his "user.dat" (with ">>" instead of ">", like he mentioned at
some point), then at the end of the existing script,
    echo "User        Number of Processes"; echo; \
    grep -v "^USER" < user.dat | sort | uniq -w 8 -c | \
    awk "{printf \"%8-s%15d\n\",\$2,\$1}"

    - Dan


Quote:
> > $ ps aux | sort | uniq -w 8 -c

> Nice use of other programs and the shell, but even though the information
> itself is there, it's still wrong.  The original poster specified the
format
> of the output.  All this does is change the problem from formatting ps
> output, to formatting uniq output (that erroneously includes the header
line
> from ps).  (Besides, the OP is trying to learn awk, not the shell and
> standard filters.)

> But it does move the awk side to a place where the OP won't need to learn
> about arrays (even though I thought that was his point).  This is closer:

> echo "User        Number of Processes"; echo; ps aux | grep -v "^USER" |
> sort | uniq -w 8 -c | awk "{printf \"%8-s%15d\n\",\$2,\$1}"

> at least with my bash and ps on linux. (This should be one line)  On my
> machine this yeilds:

> User        Number of Processes

> bin                   1
> daemon                1
> nobody                1
> root                 37
> xfs                   1

> And, since I used awk, I'm back on-topic ;)

>     - Dan



> > David Sienkiewicz





> > >> % Given a script that runs a ps -ef command to get a sampling of
> > >> % processes running on a Unix system, I'm to come up with an AWK
> > >> script % that finds each unique process, counts them, and
> > >> displays a table like % this:
> > >> %
> > >> % User        Number of Processes
> > >> %
> > >> % User1                 3
> > >> % User2                16
> > >> % User3                 9

> > [..]

> > $ ps aux | sort | uniq -w 8 -c

> > Good luck

> > Michael Heiming
> > --
> > Remove the +SIGNS case mail bounces.



Fri, 08 Oct 2004 03:47:17 GMT  
 An AWK poser that's stumped a novice

[..]

Quote:
> echo "User        Number of Processes"; echo; ps aux | grep -v
> "^USER" | sort | uniq -w 8 -c | awk "{printf
> \"%8-s%15d\n\",\$2,\$1}"

[..]

Looks for me like useless use of echo + grep. One! line:

$ ps aux | sort | uniq -w 8 -c | awk 'BEGIN{print "User        
Number of Processes"}{if ($2!=/USER/) printf "%8-s%15d\n",$2,$1}'

Michael Heiming
--
Remove the +SIGNS case mail bounces.



Fri, 08 Oct 2004 03:33:36 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. RB novice stumped

2. Novice Ada Programmer Stumped By Simple Type Declaration!

3. Stupid questions from awk novice

4. Okay, I'm stumped.

5. error 19 - i'm stumped - snip [1/1]

6. I'm Stumped ...

7. I'm stumped on threads

8. Problems installing pythonwin - I'm stumped

9. [Fwd: Re: I'm stumped on threads]

10. proc global problem - I'm completely stumped

11. I'm stumped, need help

12. expect u question - i'm stumped - completely...

 

 
Powered by phpBB® Forum Software