views:

471

answers:

10

I have one question maybe someone here can help me. If i do "ps aux --sort user" on linux console I have one list of users and their processes runing on the machine. My question is how do I remove the users name and print that list like this in a C program:

for example:

(…)
--------------------------------------------------------------------------
user: APACHE
--------------------------------------------------------------------------
3169 0.0 1.2 39752 12352 ? S 04:10 0:00 /usr/sbin/httpd
--------------------------------------------------------------------------
user: VASCO
--------------------------------------------------------------------------
23030 0.0 0.1 4648 1536 pts/1 Ss 20:02 0:00 –bash

(…)

I print the user name then I print his processes... any ideas ?

thx

+3  A: 
ps aux --sort user | perl -npe 's/^(\w+)//g; if ($user ne $1) {$user=$1; print "user: " . uc($user) . "\n";}'
bmdhacks
That's not quite right, because \w matches alphanumberics and underscore, but other characters are valid in user names. In particular hyphen is very common (e.g. www-data)
Mark Baker
A: 

Not really an answer to your question, but user names are case-sensitive in unix, so capitalising them all probably isn't a good idea. If you want to make them stand out visually then "USER: apache" would be better.

Apart from that bmdhacks' answer is good (but not quite right). You could do something similar in awk, but it would be rather more complicated.

Mark Baker
A: 

This should work:

ps haux --sort user | perl -npe 's/^(\S+)\s+//; if ($user ne $1) {$user=$1; print "user: " . uc($user) . "\n";}'

Based on bmdhacks's answer, but with the following fixes:

a) it counts any non-whitespace as part of the user name, b) it deletes any whitespace after the user name, like your example output had, otherwise things wouldn't line up c) I had to remove the g to get it to work. I think because with the g it can potentially match lots of times, so perl doesn't set $1 as it could be ambiguous. d) Added h to the ps command so that it doesn't output a header.

Mark Baker
Why use perl when you can use, say, awk?
Nick Stinemates
I tried it in awk and it's more complicated. Possible, but the perl version is much easier to read, honestly.
Mark Baker
Why not try it in awk and post it here? You might be better at awk than me (very likely!) and I think an alternative solution is always interesting.
Mark Baker
A: 

that's command line for linux to make what i said... but that's not what i want... i want to make that in some C program... I hav to wrote C program that makes that... so i use fork() to create one process that executes ps aux --sort user... and then i want with another process to control the print of the processes and users... sry, if i explain my problem rong.

The command that i want to run is like this: ps aux --sort user | sort_by_user... this option sort_by_user doesn.t exists.. Make some process in C to run that command is simple with the commands fork() and execlp(), but create some option to that command in C i don´t hav any ideas.

I've added the "C program" requirement to your original question, you really should have included that information to begin with.
Adam Bellaire
+1  A: 

You have a number of options depending on how much of it you want to do in C.

The simplest is to use system() to run a shell command (such as the one I posted earlier) to do the whole lot. system() will actually spawn a shell, so things like redirection will all work just as they do from the command line.

If you want to avoid using system() you could do it yourself, spawning two processes and linking them together. Look up pipe() and dup2(). Probably a waste of time.

You can run the ps program and parse its output in C. Again pipe() and dup2() are relevant. For the actual parsing, I'd just do it using the normal C string handling routines, as it's really quite straightforward. Obviously you could use a regex library instead but I think in this case it would result in more complicated code than without it.

Of course, you could do the whole lot in C by looking at files in /proc.

Mark Baker
A: 

Use popen and manipulate the redirected stdout string in your C program

A: 

is this homework?

Paul Nathan
A: 

yep... i already solv the problem... redirect the stdout put and edit it... thx

+1  A: 

viva a informatica no Dei. S.O é dificil!!

hahahahaha, for other ppl this is a course at the "DEI == Department of Engineering Informatics" (in portuguese makes more sence)Full translation says "hurray for informatics on dei" Operative Sistems(course) is hard
fmsf
A: 

looool... moura