LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   i want a shell script to print system processess in required format. (https://www.linuxquestions.org/questions/linux-newbie-8/i-want-a-shell-script-to-print-system-processess-in-required-format-653499/)

vpradeep 07-04-2008 02:02 AM

i want a shell script to print system processess in required format.
 
hi,
my question is:
i want to print system processes in the format as below,
eg:
1 ......
2 .....
3 .....

2256 .....

5543 .....
5544 .....

means process with sequential pids should be continuous and if there is a break a newline should be printed.please help me out.
thanks.

Mr. C. 07-04-2008 03:27 AM

You can sort ps output. man ps, search for sort. Search also for the output specifiers to use for your desired output format. There are many ps variants, so you'll have to check out your man page to see how yours works.

But, for this problem, there is no need to sort ps output with ps itself. Consider the code below. It will work if your PIDs are in column 2, and your ps accepts the -aux output. If not, change the $F[1] to $F[N], where N is the column number where your PIDs are minus 1.

ps -aux | perl -nae 'END { $i=1; map { if ($i != $_) { $i = $_; print "\n" } $i++; print "$_\n"; } sort { $a <=> $b } keys %lines } $lines{$F[1]} = @F;'

vpradeep 07-04-2008 03:47 AM

thankyou
 
thankyou,
its working.

syg00 07-04-2008 04:22 AM

mmmm - sounds like homework. LQ has rules against such.
Interesting answer all the same Mr. C. ... ;)

Mr. C. 07-04-2008 04:35 AM

I thought it might be hw too. But anyone turning this answer in for submission will be spotted.


All times are GMT -5. The time now is 01:21 AM.