0

I have done few google searches but none of them answered my question.

Do we have any universal way to display headers of all commands in Ubuntu with out installing additional packages? commands like ps -ef & ls, du.

Example

ps -ef | egrep "PID"
UID        PID  PPID  C STIME TTY          TIME CMD
root     18208 21629  0 02:49 pts/0    00:00:00 egrep --color=auto PID

in the above example, I have used egrep to get the header the above command prints no header when other grep words are used

Example below

ps -ef | egrep "nrpe"
nobody    8262     1  0 01:49 ?        00:00:00 /usr/bin/nrpe -c /etc/nrpe.cfg -d
root     18225 21629  0 02:49 pts/0    00:00:00 egrep --color=auto nrpe

P.S : I know functions of egrep but that doesn't work everywhere to get the headers.

TIA

  • its my bad i presume i didn't put the question in the right way, i don't want to segregate and print a specific header column. i just wanna know is their a universal way to only display headers of any command with out using any additional package.

    Thanks for Answers

    – preetam singh Nov 23 '19 at 17:14
  • using head command, it is universal, as explained in my answer, the example that I used is just different from yours (I use -el option instead of -ef, so column displayed are different due to his choice of options) – damadam Nov 25 '19 at 14:16

1 Answers1

2

Use a double pipe command, with head

ps -el | head -1 && ps -el | grep bash
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1001  2680  2671  0  80   0 -  7532 wait   pts/0    00:00:00 bash
0 S  1001  3673  2671  0  80   0 -  7499 wait   pts/1    00:00:00 bash
4 S  1000  3694  3681  0  80   0 -  7469 -      pts/1    00:00:00 bash
0 S  1001  6825  2671  0  80   0 -  7499 wait   pts/2    00:00:00 bash

Explanation :

  • ps -el is the command that you would get result
  • head -1 will print the 1rst line displayed by your command (here, ps -el), so it would be the header line; the number of the option -1 just say how many lines must be displayed, so if your header is 2 lines, use -2 instead
  • there is the grep of your command
  • && help you to execute 2 commands in 1 line :
    1. it show the header line thanks to head -1
    2. it display the ps result filtered by the grep command

Also, there is a contracted form (which is equivalent) :

ps -el | ( head -1 && grep bash )

NB : if you also want to have the header line at the end (due to some result), just add another && statement, to do a 3 commands in 1 line, like that :

ps -el | head -1 && ps -el | grep bash && ps -el | head -1

Result :

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1001  2680  2671  0  80   0 -  7532 wait   pts/0    00:00:00 bash
0 S  1001  3673  2671  0  80   0 -  7499 wait   pts/1    00:00:00 bash
4 S  1000  3694  3681  0  80   0 -  7469 -      pts/1    00:00:00 bash
0 S  1001  6825  2671  0  80   0 -  7499 wait   pts/2    00:00:00 bash
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
damadam
  • 2,833