0

I can use lsof to get top open files stat for processes, as below:

$ lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr | head -n 5
  17955 11945
  10282 2786
   5980 32152
   1920 27803
   1786 32107

Now I want to expand the result to like below with one line bash command,

$ lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr | head -n 5 ...
  17955 11945  java
  10282 2786   python
   5980 32152  ruby
   1920 27803  go
   1786 32107  rust

How can I achieve this?

Y.Huang
  • 11
  • 1

1 Answers1

2

If you use awk to print $1 (the command) and $2 (the PID) in reverse order ($2, $1) this provides most of the information you want in a usable format. The first sort and uniq -c still work as expected which leaves you with an unsorted list

<count> <PID> <command>

Now you just need to modify your final sort -rn ... to sort only on <count> which is trivial.

You should probably remove the initial line from the output of lsof too.

user9517
  • 115,471
  • 20
  • 215
  • 297