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 PIDin 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 nrpeP.S : I know functions of egrep but that doesn't work everywhere to get the headers.
TIA
21 Answer
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 bashExplanation :
ps -elis the command that you would get resulthead -1will print the 1rst line displayed by your command (here,ps -el), so it would be the header line; the number of the option-1just say how many lines must be displayed, so if your header is 2 lines, use-2instead- there is the
grepof your command &&help you to execute 2 commands in 1 line :- it show the header line thanks to
head -1 - it display the
psresult filtered by thegrepcommand
- it show the header line thanks to
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 -1Result :
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 3