How to grep ps output with headers
Solution 1:
ps -ef | egrep "GMC|PID"
Replace the "GMC" and ps
switches as needed.
Example output:
root@xxxxx:~$ ps -ef | egrep "disk|PID"
UID PID PPID C STIME TTY TIME CMD
paremh1 12501 12466 0 18:31 pts/1 00:00:00 egrep disk|PID
root 14936 1 0 Apr26 ? 00:02:11 /usr/lib/udisks/udisks-daemon
root 14937 14936 0 Apr26 ? 00:00:03 udisks-daemon: not polling any devices
Solution 2:
Thanks to geekosaur, I would like to use this command for your demands, rather than a separated command:
ps -ef | head -1; ps -ef | grep "your-pattern-goes-here"
The tricky is to make use of the ";" supported by the shell to chain the command.
Solution 3:
Second column is the process id; 4th is when the process was created (this is usually the time your program started, but not always; consider execve()
and friends); 6th is the amount of CPU time consumed. So it's been running for 8 days and used almost 7 days of CPU time, which I would consider worrisome.
Getting the header in the same invocation is tricky at best; I'd just do a separate ps | head -1
. You might consider using ps
's own selection methods or something like pgrep
instead of grep
, which isn't really designed to pass headers through.
Solution 4:
The egrep solution is simple and useful, but of course you depend on the header always containing 'PID' (a more than reasonable assumption, though) and the same string not ocurring elsewhere. I'm guessing this is enough for your needs, but in case someone wants an alternative there's sed.
Sed lets you just say "print the first line, then any line containing the pattern". For example:
ps auxwww | sed -n '1p; /PROCESS_NAME_TO_SEARCH/p;'
Add /sed -n/d;
to filter sed itself out:
ps auxwww | sed -n '1p; /sed -n/d; /PROCESS_NAME_TO_SEARCH/p;'
Solution 5:
easier alternative:
ps -ef | { head -1; grep GMC; }
replace the number with the number of lines your header is displayed on.