Match with an external team

I wrote a little script using sed to convert this:

kaefert@Ultrablech ~ $ cat /sys/class/power_supply/BAT0/uevent
POWER_SUPPLY_NAME=BAT0
POWER_SUPPLY_STATUS=Full
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-ion
POWER_SUPPLY_CYCLE_COUNT=0
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=7400000
POWER_SUPPLY_VOLTAGE_NOW=8370000
POWER_SUPPLY_POWER_NOW=0
POWER_SUPPLY_ENERGY_FULL_DESIGN=45640000
POWER_SUPPLY_ENERGY_FULL=44541000
POWER_SUPPLY_ENERGY_NOW=44541000
POWER_SUPPLY_MODEL_NAME=UX32-65
POWER_SUPPLY_MANUFACTURER=ASUSTeK
POWER_SUPPLY_SERIAL_NUMBER= 

to csv file format, for example:

kaefert@Ultrablech ~ $ Documents/Asus\ Zenbook\ UX32VD/power_to_csv.sh 
"date";"status";"voltage µV";"power µW";"energy full µWh";"energy now µWh"
2012-07-30 11:29:01;"Full";8369000;0;44541000;44541000 
2012-07-30 11:29:02;"Full";8369000;0;44541000;44541000 
2012-07-30 11:29:04;"Full";8369000;0;44541000;44541000
... (in a loop)

Now I would like to divide each of these numbers by 1.000.000 so that they do not represent μV, and V and W instead of μW, so that they are easily interpreted with a quick look. Of course, I could do it manually after I opened this csv inside libre office calc, but I would like to automate it.

So, I found that I can call external programs between sed, for example:

...
s/\nPOWER_SUPPLY_PRESENT=1\nPOWER_SUPPLY_TECHNOLOGY=Li-ion\nPOWER_SUPPLY_CYCLE_COUNT=0\nPOWER_SUPPLY_VOLTAGE_MIN_DESIGN=7400000\nPOWER_SUPPLY_VOLTAGE_NOW=\([0-9]\{1,\}\)/";'`echo 0`'\1/

and that I can get values ​​like I want, something like this:

echo "scale=6;3094030/1000000" | bc | sed 's/0\{1,\}$//'

But now the problem is how to pass the match "\ 1" to an external command?

If you are interested in viewing the full script, you will find it there: http://koega.no-ip.org/mediawiki/index.php/Battery_info_to_csv

+5
3

sed GNU sed. 'e' / sed.

:

, :
"120+20foobar", 120 + 20 "oo" "xx" "foobar".

, , sed 'e'

120+20 , /, . :

   kent$  echo "100+20foobar"|sed -r 's#([0-9+]*)(.*)#echo  \1 \|bc\;echo \2 \| sed "s/oo/xx/g"#ge'
    120
    fxxbar

, , .: D

+6

sed , awk - , . 3-, 5- 6- - :

 awk -F';' -v OFS=';' '
   NR == 1
   NR != 1 { 
     $3 /= 1e6
     $5 /= 1e6
     $6 /= 1e6
     print
   }'

  • -F';' -v OFS=';' .
  • NR == 1 .
  • NR != 1, , .
+2

1 000 000 , :

Q='3094030/1000000'
sed ':r /^[[:digit:]]\{7\}/{s$\([[:digit:]]*\)\([[:digit:]]\{6\}\)/1000000$\1.\2$;p;d};s:^:0:;br;d'
0
source

All Articles