Search code examples
linuxloggingmemoryraspbianfree

Transpose output of free


I am interested in logging the output of free and add a timestamp. I would like an output similar to this however I would like the final output to have these columns

date, time, Mem. total, Mem. used, Mem. free, Swap total, Swap used, swap, free. 

So that the two types of memory appear in the same line. In contrast free reports mem and swap in 2 lines.

If this is not possible the other option would be

date, time, type of memory, total, used, free

where type of memory would be mem or swap.

Is something like this possible? As a script so as to call it every 5 min with cron?


Solution

  • Does the following work for you?

    (date "+%Y%m%d,%H%M%S"; free -wm )| awk 'BEGIN{OFS=","} NR==1{printf "%s",$0} NR==3{$1="";printf "%s,",$0} NR==4{$1="";printf "%s",$0} END{print ""}'