Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following script:

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

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

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:


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
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)

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 '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 '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 '14 at 16:59
@Rumanoid edit made. – Aaron Feb 14 '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. – Aaron Feb 14 '14 at 16:22

Your Answer


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.