Operating on each of the individual outputs of a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Operating on each of the individual outputs of a command
# 1  
Old 11-07-2009
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.
# 2  
Old 11-07-2009
Quote:
Originally Posted by proactiveaditya
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  
Old 11-08-2009
Quote:
Originally Posted by proactiveaditya
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Need command to find missing operating system security patches

Hi, Need a command to find out Missing operating system patches for AIX 7.1 or a command to list down all installed patches. (1 Reply)
Discussion started by: Mona Khandelwal
1 Replies

2. Shell Programming and Scripting

Time outputs of wget command

Hello friends, I've been working on a solaris server, I need to test responses of a web service using WGET command, if the response is successful, how quick it is etc. I have scirpt like this, I modified it, i try to redirect the output of time command to total.txt but i couldn't manage, i... (4 Replies)
Discussion started by: EAGL€
4 Replies

3. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

4. Solaris

Reboot solaris box(What are all the command outputs)

Hi What are all the command outputs we have to note and keep it for safe before rebooting or shutting down a solaris box (5 Replies)
Discussion started by: newtoaixos
5 Replies

5. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

6. Shell Programming and Scripting

create outputs from other command outputs

hi friends, The code: i=1 while do filename=`/usr/bin/ls -l| awk '{ print $9}'` echo $filename>>summary.csv #Gives the name of the file stored at column 9 count=`wc -l $filename | awk '{print $1}'` echo $count>>summary.csv #Gives just the count of lines of file "filename" i=`expr... (1 Reply)
Discussion started by: rajsharma
1 Replies

7. Shell Programming and Scripting

How to store multiple outputs from an awk command?

x=`echo $line | awk -F "|" '{print $1;print NR}'` How will I get the 2 return values ($1 and NR) from awk to variables? (4 Replies)
Discussion started by: tene
4 Replies

8. Shell Programming and Scripting

Assigning variable from command outputs to shell

First, this is bash (3.2.17), on a Mac, 10.5.7. What I'm trying to do is look at a list of users, and check to see if each exists. If they do, do some more stuff, if they don't, drop them into an error file. So, my user list is: foo - exists bar - does not exist blah - does not exist ... (2 Replies)
Discussion started by: staze
2 Replies

9. Shell Programming and Scripting

Trouble with tee command to capture script outputs

function GetInput { print -n "Input" read input export INPUT=$input } export COMMAND="GetInput" $COMMAND echo "$INPUT" $COMMAND | tee -a Log.log echo "$INPUT" The first one without "tee" works fine. echo "$INPUT" displays the values I type in for input. The second... (5 Replies)
Discussion started by: muthubharadwaj
5 Replies

10. Shell Programming and Scripting

recording command outputs in background

For example: % ls /store > list.txt % less list.txt % 1.txt % 2.txt I want to execute 'ls' in verbose and store the result in background to 'list.txt' so i dont have to execute another command just to view the contents of the 'list.txt' % ls /store > list.txt % 1.txt % 2.txt %... (2 Replies)
Discussion started by: jehrome_rando
2 Replies
Login or Register to Ask a Question