capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command is

echo date
`top -b -n1 | grep -e Cpu -e Mem`

I get the output in 3 separate lines.

Tue Feb 24 15:00:03
Cpu(s): 3.4% us, 8.5% sy .. ..
Mem: 1011480k total, 226928k used, ...

. My desired output is

Tue Feb 24 15:00:03 3.4% 8.5% ... 1011480k 226928k

I have tried to use awk to process the output from the grep

tob -b -n1 | grep -e Cpu -e Mem | awk '{print $2 $4

and I only the figures I required, but the output still spans 3 lines

2.8% 7.0%
1011480k 227896k

How should I modify the awk command to get everything on one line with the date at the front ?

date| cut -d" " -f1-4; top -b -n1 | awk '/^Cpu/ {print $2,$4,$6,$8} /^Mem/ {print $2,$4}'

Hi zaxxon, thank you for your prompt response.

I followed your suggestion and i got the output in 3 lines. Any suggestions to get the 3 lines combined into 1 line?

A=`date| cut -d" " -f1-4; top -b -n1 | awk '/^Cpu/ {print $2,$4,$6,$8} /^Mem/ {print $2,$4}'`
echo $A
Di 24. Feb 12:27:50 0.1% 0.0% 0.1% 0.0% 1003060k 982620k
1 Like

Hi zaxxon,

thank you for your suggestion.