LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-04-2008, 03:02 AM   #1
vpradeep
LQ Newbie
 
Registered: Jun 2008
Location: bangalore
Posts: 4

Rep: Reputation: 0
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.
 
Old 07-04-2008, 04:27 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
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;'

Last edited by Mr. C.; 07-04-2008 at 04:28 AM.
 
Old 07-04-2008, 04:47 AM   #3
vpradeep
LQ Newbie
 
Registered: Jun 2008
Location: bangalore
Posts: 4

Original Poster
Rep: Reputation: 0
thankyou

thankyou,
its working.
 
Old 07-04-2008, 05:22 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,833

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
mmmm - sounds like homework. LQ has rules against such.
Interesting answer all the same Mr. C. ...
 
Old 07-04-2008, 05:35 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script assistance required.. TheEngineer Linux - Newbie 4 10-26-2006 07:42 AM
Shell Script.. Date format handling C-RAF. Programming 2 02-14-2006 09:34 AM
shell script help required syntax error sridhar11 Programming 1 10-31-2005 09:38 AM
small shell script required demigor Programming 6 08-08-2005 05:38 PM
shell script provides an error that a binary operator is required max_rsr Linux - General 1 03-12-2005 09:26 AM


All times are GMT -5. The time now is 10:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration