bash RRDTool GPRINT 格式化与 printf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11353289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RRDTool GPRINT formatting with printf
提问by general exception
Closely related to this question: Bash printf prefix
与这个问题密切相关:Bash printf prefix
I have the following Bash script that is generating an RRDGraph with RRDTool.
我有以下 Bash 脚本,它使用 RRDTool 生成 RRDGraph。
#!/bin/bash
now=$(date +%s)
now_formatted=$(date +%s | awk '{printf "%s\n", strftime("%c",)}' | sed -e 's/:/\:/g')
# create power graph for last week
/usr/bin/rrdtool graph /var/www/power-week.png \
--start end-7d --width 543 --height 267 --end $now-1min --slope-mode \
--vertical-label "Watts" --lower-limit 0 \
--alt-autoscale-max \
--title "Power: Last week vs. week before" \
--watermark "(?) $(date +%Y) Alyn R. Tiedtke" \
--font WATERMARK:8 \
DEF:Power=/root/currentcost/ccdata.rrd:Power:AVERAGE \
DEF:Power2=/root/currentcost/ccdata.rrd:Power:AVERAGE:end=$now-7d1min:start=end-7d \
VDEF:Last=Power,LAST \
VDEF:First=Power,FIRST \
VDEF:Min=Power,MINIMUM \
VDEF:Peak=Power,MAXIMUM \
VDEF:Average=Power,AVERAGE \
CDEF:kWh=Power,1000,/,168,* \
CDEF:Cost=kWh,.1029,* \
SHIFT:Power2:604800 \
LINE1:Power2#00CF00FF:"Last Week\n" \
HRULE:Min#58FAF4:"Min " \
GPRINT:Power:MIN:"%6.2lf%sW" \
COMMENT:"\n" \
LINE1:Power#005199FF:"Power " \
AREA:Power#00519933:"" \
GPRINT:Last:"%6.2lf%sW" \
COMMENT:"\n" \
HRULE:Average#9595FF:"Average" \
GPRINT:Power:AVERAGE:"%6.2lf%sW" \
COMMENT:"\n" \
HRULE:Peak#ff0000:"Peak " \
GPRINT:Power:MAX:"%6.2lf%sW" \
COMMENT:"\n" \
GPRINT:kWh:AVERAGE:" total %6.2lfkWh\n" \
GPRINT:Cost:AVERAGE:" cost %6.2lf £\n" \
GPRINT:Cost:AVERAGE:"$(printf \" cost %11s\" £%.2lf | sed 's/\£/\£ /g')\n" \
COMMENT:" \n" \
GPRINT:First:"Showing from %c\n":strftime \
GPRINT:Last:" to %c\n":strftime \
COMMENT:" Created at $now_formatted"
Which produces a graph like this (notice the leading \on the lower cost line in the legend):-
产生这样的图表(注意\图例中较低成本线上的领先):-


Concentrating specifically on the following line:-
特别关注以下几行:-
GPRINT:Cost:AVERAGE:"$(printf \" cost %11s\" £%.2lf | sed 's/\£/\£ /g')\n" \
This is the line that is printing out the lower cost line in the legend.
这是打印出图例中较低成本行的行。
I am passing a GPRINT formatted value of £4.54to Bash's printffunction to be padded out to 11 spaces and a costlabel prefixed on it. I am then piping this to sedto add a space between the £and the actual value.
我正在将 GPRINT 格式的值传递£4.54给 Bash 的printf函数,以填充到 11 个空格并cost在其上添加一个标签。然后我通过管道将sed其添加到£和实际值之间。
What I want to know is, why is the escaped \coming through in the output? If I remove the \\just after printf bash complains that something is missing.
我想知道的是,为什么\在输出中转义了?如果我\\在 printf bash 抱怨缺少某些东西之后删除它。
How would I suppress this \from coming through in the output.
我将如何抑制它\在输出中通过。
采纳答案by sarnold
Try this line:
试试这一行:
GPRINT:Cost:AVERAGE:"$(printf ' cost %11s' £%.2lf | sed 's/\£/\£ /g')\n" \
I changed the inner"marks to 'marks and removed the backslashes.
我将内部"标记更改为'标记并删除了反斜杠。

