read values from ps -ef into variables in ksh?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read values from ps -ef into variables in ksh?
# 1  
Old 01-09-2008
read values from ps -ef into variables in ksh?

Hi,

I want to get the first two items returned by ps -ef into two variables?

Can anyone please help

Thanks
# 2  
Old 01-09-2008
CPU & Memory Read ps -ef first two items in two variables

this is one of the way to do in shell script

field1=`ps -ef|grep <some pattern>|grep -v grep|awk '{print $1}'`
field2=`ps -ef|grep <some pattern>|grep -v grep|awk '{print $2}'`
# 3  
Old 01-09-2008
what are you looking for like first two items of row or column?
# 4  
Old 01-09-2008
Quote:
Originally Posted by Bhagwat
this is one of the way to do in shell script

field1=`ps -ef|grep <some pattern>|grep -v grep|awk '{print $1}'`
field2=`ps -ef|grep <some pattern>|grep -v grep|awk '{print $2}'`
to reduce the amount of pipes, you can use awk,eg
Code:
field1=`ps -ef | awk '/pattern/{print $1}'

# 5  
Old 01-09-2008
Sorry, I should have been more clear. I am doing a for loop, and for each row returned from the for loop, I want to get the first two values


Code:
for x in `ps -ef`
do

GET THE VALUES OF THE FIRST TWO ELEMENTS RETURNED BY THE CURRENT ps -ef i.e. x

done

Thanks
# 6  
Old 01-09-2008
Code:
ps -ef|awk 'NR>1 && NR<=3'

this will only get the 2 lines after the headers. Depending on what you want to do next, you can code your next step in the awk statement,

Code:
ps -ef|awk 'NR>1 && NR<=3{ #do something}'

or pipe the output to a while loop.
eg
Code:
ps -ef|awk 'NR>1 && NR<=3' | while read line 
do
 #do something
done

# 7  
Old 01-12-2008
Thank you everyone
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

4. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

5. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

6. Shell Programming and Scripting

Read variables and their values from file

Hi, I want to read the variables and the values from the txt file and compare these values with the ones computed by script. for ex: say var.txt contains the variable names and their values: one 1 two 2 three 3 The value of variables "one" "two" and "three" will be computed in the script... (3 Replies)
Discussion started by: bhushana
3 Replies

7. UNIX for Dummies Questions & Answers

trouble using read to store values in variables from command output

I know there are caveats about using read in pipelines because read is treated by a subshell. I know this but I can't think of any way to accomplish this regardless, I'm still a rookie. I hope somebody will be able to interpret what it is that I'm trying to accomplish and correct me. ... (2 Replies)
Discussion started by: ProGrammar
2 Replies

8. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

9. Shell Programming and Scripting

How to read values that are passed to the shell function in ksh.

In ksh shell, There is a function f1. function f1 { How to read here?? .... .... } I am passing values to fuunction f1 as f1 "A" "B" Please tell me how to read the passed values in function f1. Advance Thanks & Regards Prashant (2 Replies)
Discussion started by: prashant43
2 Replies

10. Shell Programming and Scripting

Cannot read variables after ssh with rc file (KSH)

Greetings all, I'm currently making use of the $HOME/.ssh/rc file to launch an automated shell script immediately after the user has been verified through ssh. The current problem that I'm facing now is that I am unable to use the "read" command anymore... seems like the "read" statements are... (0 Replies)
Discussion started by: rockysfr
0 Replies
Login or Register to Ask a Question