You are not logged in.

#1 2010-05-08 02:58:47

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

divide cat output by 60?

Hi everyone.

when i do

cat /sys/devices/platform/smapi/BAT0/remaining_running_time

i get a numeric value with 1 to 3 digits.

How can i divide the output of that file by 60 using a pipe?

Greets,
demian


no place like /home
github

Offline

#2 2010-05-08 03:05:05

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: divide cat output by 60?

bc?


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#3 2010-05-08 03:18:05

ssjlegendx
Member
Registered: 2008-01-01
Posts: 94
Website

Re: divide cat output by 60?

There is probably a cleaner way to do this, but:

$ echo "$(cat /sys/devices/platform/smapi/BAT0/remaining_running_time)/60" | bc -l

cut, sed, or awk might be useful if you want to remove trailing zeroes.

(Credit goes to this page for reminding me that the -l flag enables standard division.)


#!/vim/rocks

Offline

#4 2010-05-08 03:50:02

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: divide cat output by 60?

Thanks.
I probably should have mentioned that I want to use this within a conky configuration file. So, while your code works fine with interpreter i have trouble implementing it in conky.


no place like /home
github

Offline

#5 2010-05-08 04:05:33

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: divide cat output by 60?

Did you try exec?


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#6 2010-05-08 04:27:56

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: divide cat output by 60?

Geez. Of course. In my defense, it's 6.30 am here.

The full command:

Remaining: ${color1}${exec echo "$(cat /sys/devices/platform/smapi/BAT0/remaining_running_time)/60" | bc -l | cut -c1-3}h${color}

Last edited by demian (2010-05-08 04:28:12)


no place like /home
github

Offline

#7 2010-05-08 05:51:03

quigybo
Member
Registered: 2009-01-15
Posts: 223

Re: divide cat output by 60?

a version without bc

cat /sys/devices/platform/smapi/BAT0/remaining_running_time | awk '{print $1/60}'

Offline

#8 2010-05-08 11:46:32

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: divide cat output by 60?

Thanks.

Remaining: ${color1}${exec cat /sys/devices/platform/smapi/BAT0/remaining_running_time | awk '{print $1/60}'}h${color}

Works just as well.


no place like /home
github

Offline

#9 2010-05-08 14:51:40

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: divide cat output by 60?

But not that awk use more resource than bc. Timing then (this is only a lazy comparison)

$ time (for i in {1..1000}; cat remaining_running_time|awk '{print $1/60}' >/dev/null)
(; for i in {1..1000}; do; cat remaining_running_time | awk '{print $1/60}' >)  2.49s user 1.32s system 80% cpu 4.717 total

$ time (for i in {1..1000}; echo "$(cat remaining_running_time)/60"|bc -l >/dev/null) 
(; for i in {1..1000}; do; echo "$(cat remaining_running_time)/60" | bc -l >)  0.54s user 0.62s system 32% cpu 3.560 total

And for comparison a small "made in 10 seconds" c program:

$ time (for i in {1..1000}; ./read remaining_running_time 60 >|/dev/null) 
(; for i in {1..1000}; do; ./read remaining_running_time 60 >| /dev/null; done  0.04s user 0.20s system 19% cpu 1.210 total
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  FILE *fp = fopen(argv[1],"r");
  float time;
  fscanf(fp, "%f", &time);
  printf("%f\n", time/atof(argv[2]));
  fclose(fp);

  return 0;
}

Offline

#10 2010-05-08 15:03:21

schuay
Package Maintainer (PM)
From: Austria
Registered: 2008-08-19
Posts: 564

Re: divide cat output by 60?

$ echo $(( 60 / 3 ))
20

Offline

#11 2010-05-08 15:31:41

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: divide cat output by 60?

schuay wrote:

$ echo $(( 60 / 3 ))
20

The problem is conky call sh and sh is bash and bash dont like floating points (try $((60.0/3))), zsh run floating point ok. But with integer arithmetic is ok this is a solution

Offline

#12 2010-05-08 19:32:28

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: divide cat output by 60?

Thanks for th hint, kazuo. The difference in speed and resource consumption is impressive.


no place like /home
github

Offline

Board footer

Powered by FluxBB