Hi,
I want to get the first two items returned by ps -ef into two variables?
Can anyone please help
Thanks
Hi,
I want to get the first two items returned by ps -ef into two variables?
Can anyone please help
Thanks
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}'`
what are you looking for like first two items of row or column?
to reduce the amount of pipes, you can use awk,eg
field1=`ps -ef | awk '/pattern/{print $1}'
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
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
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,
ps -ef|awk 'NR>1 && NR<=3{ #do something}'
or pipe the output to a while loop.
eg
ps -ef|awk 'NR>1 && NR<=3' | while read line
do
#do something
done
Thank you everyone
ps -ef | head -3 | tail -2
if the ps version supports no headers
ps -ef --no-headers| head -2