Reputation: 488
I paid attention that 'grep' command removes the column names. I need to customize the output of processes according to the following command:
ps -ef | egrep "java|mysql" | awk {'print $1, $2, $8'}
Regular 'ps' (or even with the 'awk') have column names: UID, PID, etc... However, when i add 'grep' the column names gone. Ideally i must have the 'ps' output that displays 4 columns - PID, user name, CMD, and memory usage. How do i get it preserving the column names.
Upvotes: 1
Views: 3332
Reputation: 1419
Use the power of bash
.
$ cat pid_top.sh
#!/bin/bash
echo "Enter String:"
read p
top -n 1 -b -p $(ps -e | grep $p | awk '{print $1}') | tail -n 2
Don't forget chmod +x pid_top.sh
Make an alias or run the script by name, enter a string, e.g. for firefox just fire an you will have and output like this:
$ ./p*
Enter String:
fire
PID USER PR NI VIRT RES SHR S %CPU %MEM ZEIT+ BEFEHL
2983 bang 20 0 1605624 600168 108940 S 0,0 15,1 40:28.10 firefox
Upvotes: 0
Reputation: 75629
The grep
command will strip the headers because they do not match. Just use awk
and match on the first row condition in addition to the search patterns.
ps -ef | awk 'NR==1{print $1,$2,$8} /java|mysql/{print $1, $2, $8}'
Upvotes: 1