Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have the following script:

    while :
 do
        clear;
        echo "---------------------------RAM Load------------------------------$
        free -mt >> memory.txt;
        free -mt;
        echo "---------------------------Temperature---------------------------$
        sensors >> temp.txt;
        sensors;
        sleep 1;
        clear;
done

which gets the current system RAM and temperature and writes it to two files, respectively memory.txt and temp.txt

I want to use this data on a webserver with Dygraphs what is the best way to clean up the data, because it gives these two file:

memory.txt temp.txt

for the RAM part I only want the -/+ buffers/cache: line and from the temperature part only the temperatures.

share|improve this question

2 Answers 2

You should be able to solve this issue by using grep.

To get just the -/+ buffers/cache line, alter your free commands to this:

free -mt | grep buffers/cache >> memory.txt;
free -mt | grep buffers/cache;

To get only the temperatures try:

sensors | grep °C >> temp.txt;
sensors | grep °C;

So using the following script:

#!/bin/bash

clear;
echo "---------------------------RAM Load------------------------------$"
free -mt | grep buffers/cache >> memory.txt;
free -mt | grep buffers/cache;
echo "---------------------------Temperature---------------------------$"
sensors | grep °C >> temp.txt;
sensors | grep °C;

Produces this output:

---------------------------RAM Load------------------------------$
-/+ buffers/cache:       1545        449
---------------------------Temperature---------------------------$
Physical id 0:      N/A  (high = +100.0°C, crit = +100.0°C)
Core 0:             N/A  (high = +100.0°C, crit = +100.0°C)
Physical id 2:      N/A  (high = +100.0°C, crit = +100.0°C)
Core 0:             N/A  (high = +100.0°C, crit = +100.0°C)
aploetz@dockingBay94:~$

For removing the extra text that you don't want, you could try using awk.

$ free -mt | grep buffers/cache | awk '{print $3"\t"$4}'
1588    406

The temperatures will be a bit trickier, but can be done with tr.

$ sensors | grep Physical | tr -d '(),' | awk '{print $7"\t"$10}'
+100.0°C    +100.0°C
+100.0°C    +100.0°C

$sensors | grep Core | tr -d '(),' | awk '{print $6"\t"$9}'
+100.0°C    +100.0°C
+100.0°C    +100.0°C

If you wanted to get rid of the plusses as well, just add '+' to the delete flag:

$ sensors | grep Core | tr -d '(),+' | awk '{print $6"\t"$9}'
100.0°C 100.0°C
100.0°C 100.0°C
share|improve this answer
    
nice answer, but can I also remove the -/+ buffers/cache text and the same with the temperature, for use with Dygraph, as I stated? –  Rumanoid Feb 14 at 16:41
    
Sure try free -mt |grep buffers/cache |sed 's/^.\+ buffers\/cache: \+//' >> temp.txt, this will just output the two numbers. –  roadmr Feb 14 at 16:52
    
@roadmr and for the temperature? And is there anyway I can also get the current time for a Dygraph graph? –  Rumanoid Feb 14 at 16:59
    
@Rumanoid edit made. –  BryceAtNetwork23 Feb 14 at 17:09

In addition to Bryce's good advice, there's no need to run the command twice:

free -mt | grep buffers/cache | tee -a memory.txt
sensors | grep °C | tee -a temp.txt
share|improve this answer
    
+1 Nicely done! I forgot all about tee. –  BryceAtNetwork23 Feb 14 at 16:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.