Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

324
Vistas
Getting the first 5 lines from PID and COMMAND using top

hi i'm struggling to figure out how to get the top 5 lines of the PID and COMMAND headers when opening top

at the moment im using

top | awk '{print $1,$2}NR==5{exit}'

this is obviously annoyingly giving me this:

Processes: 390
2021/12/17 13:47:48
Load Avg:
CPU usage:
SharedLibs: 146M
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

With your shown samples, please try following code. Since you need top 5 PIDs details so printing first 5 lines wouldn't work here. So skipping first 6 lines in output of top(which are about system details).

top -b | awk 'FNR>=7 && FNR<=12{print $1};FNR==12{exit}'

Explanation: Simple explanation of above code would be, passing top command's output to awk as an standard input. Then in awk program checking condition if line number is from 7th to 12th then print it and on 12th line exit out of program.

Where definition of top -b option is as follows from man top:

b :Batch-mode operation Starts top in Batch mode, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the `-n' command-line option or until killed.

over 4 years ago · Santiago Trujillo Denunciar

0

Other Solutions:

top -b for batch mode
top -n 1 for a single shot (no loop)

Getting lines

  • with --> head -12 | tail -6
  • or --> awk 'FNR>=7 && FNR<=12'
  • or --> sed -n 7,12p

Print PID and Command

  • awk '{print $1, $NF}'

...

top -b|head -12|tail -6|awk '{print $1, $NF}'
top -b -n 1|awk 'FNR>=7 && FNR<=12{print $1, $NF}'
top -b -n 1|sed -n 7,12p|awk '{print $1, $NF}'
         


         
over 4 years ago · Santiago Trujillo Denunciar

0

For MAC OS X, the command is:

top -a -n20 | awk 'FNR>=11 && FNR<=31{print $0};FNR==31{exit}' > cpustat.txt

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda