Reputation: 43999
Trying to parse BSD top
output to show only PID - COMMAND - MEM
for a specific process;
$ top -l 1 | grep -E '%CPU\ |coreaudio'
PID COMMAND %CPU TIME #TH #WQ #PORTS MEM PURG
354 com.apple.audio. 0.0 00:00.00 2 1 12 820K 0B
296 com.apple.audio. 0.0 00:00.03 2 1 38 2024K 0B
282 coreaudiod 0.0 03:25.05 94 2 736 21M 0B
Using awk to show only column $1 - $2
$ top -l 1 | grep -E '%CPU\ |coreaudio' | awk {'print $1" -- "$2'}
PID -- COMMAND
354 -- com.apple.audio.
296 -- com.apple.audio.
282 -- coreaudiod
Adding a 3th column messes with the 'columns' since the second columns is not being padded;
$ top -l 1 | grep -E '%CPU\ |coreaudio' | awk {'print $1" -- "$2" -- "$8'}
PID -- COMMAND -- MEM
354 -- com.apple.audio. -- 820K
296 -- com.apple.audio. -- 2024K
282 -- coreaudiod -- 21M
How would I 'pad' the 'column' to keep the 'layout' intact? Or should I use a different tool like sed ?
Note;
Using top -l 1
since I'm on a Mac
Upvotes: 1
Views: 176
Reputation: 141135
You can pad the strings with some "constant" amount of spaces:
<<<$input awk '{printf "%-20s %-10s %-4s\n", $1, $2, $8}'
# ^^ ^^ ^ field width
# ^ ^ ^ left justify
You can use column
:
<<<$input awk '{print $1, $2, $8}' | column -t
Upvotes: 2