Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write into array and display in ascending order

Status
Not open for further replies.

tmistry4

Programmer
May 16, 2001
1
GB
I've written a script that display certain parent and child process IDs. Problem i'm having is writting the parent data into an array and displaying it in ascending order.

This is the function i wrote that outputs the parent data. How do i put the data i get from this function into an array and sort it in ascending order ? Thanks

ps -ef -o ppid,pid,user,args | tail +2 |awk '

function getinfo(PROCID, n) {
# print "getinfo: PROCID=" PROCID
# print "ppids: " ppids[1]
if ((PROCID+1) !=1) {
n = split(pidinfo[PROCID],ppids, " ")
print ppids[1], ppidinfo[ppids[1]]
getinfo(ppids[1])
}

}

{procs[$1] = procs[$1] " " $2
ppidinfo[$2] = $3 " " $4
pidinfo[$2] = $1
pidinfor[$2] = $3 " " $4

}

END {
print FID,pidinfo[FID],ppidinfo[FID]
getinfo(FID)
} ' FID=${1:-0}
 
Hi tmistry4,

I suppose you should pipe your output into sort:

< your script here>
}' FID=${1} | sort -k 2,2 -k 10,10 |
awk '

< continue processing data if necessary >

}' > outfile

In the example above the first -k keys primarily on
the second field and the second -k keys on field
10. The notation 2,2 means look at only the data
in field 2 then consider (10,10) only the data in
field 10. The sort program will not consider any
other data in the record.

So, only you know what the output of your script is
structured like, so, edit the sort command to suit.

If you only need to sort, then you are done. But, if
you need to actually put the sorted data in an array,
then continue by piping into awk once more as I
show above.

Good luck with this!


flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top