问题描述
我一直在使用sed来改造这个写了一个小脚本:
I have written a little script using sed to transform 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=
到这样一个CSV文件格式:
into a csv file format like this:
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)
我现在想是1.000.000每个这些数字的划分,让他们不要再presentμV,但V和W代替μW,让他们在轻松间preTABLE快速浏览。当然,我能做到这一点之后手动一旦我开了自由报办公室计算这里面的CSV,但我想它使自动化
What I would like now is to divide each of those numbers by 1.000.000 so that they don't represent µV but V and W instead of µW, so that they are easily interpretable on a quick glance. Of course I could do this manually afterwards once I've opened this csv inside libre office calc, but I would like to automatize it.
所以,我发现的是,我可以调用在sed之间的外部程序,例如:
So what I found is, that I can call external programs in between sed, like this:
...
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 could get values like I want by something like this:
echo "scale=6;3094030/1000000" | bc | sed 's/0\{1,\}$//'
但现在的问题是,我该如何通过我的比赛\\ 1到外部命令?
But the problem now is, how do I pass my match "\1" into the external command?
如果您有兴趣看完整剧本,你会在那里找到它:
If you are interested in looking at the full script, you'll find it there:http://koega.no-ip.org/mediawiki/index.php/Battery_info_to_csv
推荐答案
如果你的sed是GNU sed的。你可以用E来匹配的组/ sed的命令中传递到外部命令的工具。
if your sed is GNU sed. you can use 'e' to pass matched group to external command/tools within sed command.
一个例子可能有助于更清晰:
an example might be helpful to make it clear:
比如说,你有一个问题:结果
你有120 + 20foobar
现在你要得到120 + 20部分的计算结果的字符串,并取代面向对象到foobar的一部分XX 。
say, you have a problem:
you have a string "120+20foobar"
now you want to get the calculation result of 120+20 part, and replace "oo" to "xx" in "foobar" part.
请注意,这个例子是不是解决上述的问题,只是为
显示了sed的'E'使用
所以你可以让 120 + 20
第一场比赛组,其余的在第二组中,然后通过两组不同的命令/工具,然后得到的结果。这样的:
so you could make 120+20
in the first match group, and rest in 2nd group, then pass two groups to different command/tools and then get the result. like:
kent$ echo "100+20foobar"|sed -r 's#([0-9+]*)(.*)#echo \1 \|bc\;echo \2 \| sed "s/oo/xx/g"#ge'
120
fxxbar
在这种方式,你可以窝很多SEDS之一,一个又一个,直到你迷路。 :D
in this way, you could nest many seds one in another one, till you get lost. :D
这篇关于SED - 通匹配外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!