Get performance data (RAM / CPU) on a single line

Viewed 53

I am trying to get these returns using the free and sar command to keep track of the returns which I can then export to excel. So I need every time I run the script to create a line and jump to the next one, but since there are several commands, either put one per line or if I put a tr '\ n' '', no line break in the document.

The other option would be to enter date, hostname and returns in the AWK by means of variable but I have not been able to be recognized by this option:

I need that:

230720 13:58:05  tapi1lll 20366080 519744 97.51
230720 13:58:05  tapi1lll 20366080 519744 97.51
230720 13:58:05  tapi1lll 20366080 519744 97.51
 awk -v ' "maquina=$(hostname) -v "fecha=$(date +"%d%m%y %H:%M:%S") /Mem/ { print $3,$4; printf("%.2f \n", $3/$2 * 100.0) } END { print " " }  ' 

#! /bin/bash


ram(){

date +"%d%m%y %H:%M:%S "  ;
 hostname  ;
free | awk '  /Mem/  { print $3,$4; printf("%.2f \n", $3/$2 * 100.0) } END { print " " }  ' 



}

ram  |  tr '\n' ' '

thank you!

1 Answers

A simplest solution sometimes used would be to just use command substitution that removes empty trailing newlines and put it all inside an echo

echo "$(date ...) $(hostname) $(free | awk ...)"

Alternatively you could join lines with space:

{ date ...; hostname; free | awk ...; } | paste -sd ' '
Related