Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-07-2009
Registered User
 

Join Date: Sep 2009
Location: Kolkata,India
Posts: 201
Operating on each of the individual outputs of a command


Code:

ps -e -o pcpu,pid,cmd --sort pcpu | sed '/^ 0.0 /d'|awk '{print $2}'|grep -v PID

Gives the output:

Code:

4482
4023
5912

I want to operate on each pid in the output. How to do so.
Sponsored Links
  #2 (permalink)  
Old 11-07-2009
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,533
Quote:
Originally Posted by proactiveaditya View Post
I want to operate on each pid in the output. How to do so.

That depends on what you want to do.

If you can do it with awk:

Code:

ps -e -o pcpu,pid,cmd --sort pcpu |
 awk '
 /^ 0.0/ || /PID/ { next }
 {
    ## do whatever with $2
}'

Or, you can do it with the shell:

Code:

ps -e -o pcpu,pid,cmd --sort pcpu |
 while read pcpu pid cmd
 do
   case $pcpu$pid in
    0.0*| *PID) continue;;
   esac
   ## do whatever with  $pid, e.g.:
   echo $(( $pid * 2 ))
 done

  #3 (permalink)  
Old 11-08-2009
frans's Avatar
Registered User
 

Join Date: Oct 2009
Location: Drôme, France
Posts: 299
Quote:
Originally Posted by proactiveaditya View Post

Code:

ps -e -o pcpu,pid,cmd --sort pcpu | sed '/^ 0.0 /d'|awk '{print $2}'|grep -v PID

Gives the output:

Code:

4482
4023
5912

I want to operate on each pid in the output. How to do so.

Code:

for PID in $(ps -e -o pcpu,pid,cmd --sort pcpu | sed '/^ 0.0 /d'|awk '{print $2}'|grep -v PID)
do
    echo "the PID is $PID" # or whatever you want to do with that PID
done

Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assigning variable from command outputs to shell staze Shell Programming and Scripting 2 06-28-2009 12:40 AM
Trouble with tee command to capture script outputs muthubharadwaj Shell Programming and Scripting 5 02-16-2009 08:29 PM
recording command outputs in background jehrome_rando Shell Programming and Scripting 2 02-16-2009 05:13 PM
Need help operating the "vi" command remotely Terrible UNIX for Advanced & Expert Users 4 06-13-2006 12:54 PM



All times are GMT -4. The time now is 02:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0