简体   繁体   English

gnuplot awk/bash escaping 引用

[英]gnuplot awk/bash escaping quoting

UPDATED:更新:

I have this awk command I want to nest into a system("") command.我有这个 awk 命令我想嵌套到系统(“”)命令中。 awk ' BEGIN {yr=2016};/^#/ && NF;=0 { for(i=2;i<=NF;i++) a[i]+=$i } END { for (y=2.y<=NF;y++) print yr++ " - (" a[y] " Loads)" }' vol.dat

so I tried this: system("awk ' BEGIN {yr=2016};/^#/ && NF;=0 { for(i=2;i<=NF;i++) a[i]+=$i } END { for (y=2.y<=NF;y++) print yr++ \" - (\" a[y] \" Loads)\" }' vol.dat")所以我尝试了这个: system("awk ' BEGIN {yr=2016};/^#/ && NF;=0 { for(i=2;i<=NF;i++) a[i]+=$i } END { for (y=2.y<=NF;y++) print yr++ \" - (\" a[y] \" Loads)\" }' vol.dat")

Bash does not like it due to the nesting and I am obviously not escaping the quotes correctly. Bash 由于嵌套而不喜欢它,我显然不是 escaping 正确的引号。 However I try to escape the characters I can not get it to print with a space.但是,我尝试转义无法使用空格打印的字符。

Here is some sample input, but as I said it is not an awk problem it is an issue with the awk command being nested in double quotes and escape characters:这是一些示例输入,但正如我所说,这不是 awk 问题,这是 awk 命令嵌套在双引号和转义字符中的问题:

vol.dat
Jan 0   165 165 228 78  
Feb 10  52  149 196 79  
Mar 46  186 159 137 182

Output:
2016 (56 Loads)
2017 (403 Loads)
2018 (473 Loads)
2019 (561 Loads)
2020 (339 Loads)

I don't have gnuplot so couldn't test it based on your question changed the awk command as follows,could you please try following.我没有gnuplot,所以无法根据您的问题对其进行测试,将awk命令更改如下,请您尝试以下操作。

totals = system("awk ' BEGIN {yr=2016};/^#/ && NF;=0 { for(i=2;i<=NF;i++) a[i]+=$i } END { for (y=2.y<=NF;y++) print yr++ OFS a[y] OFS \"Loads)\" }' vol.dat")

We could actually use comma in print statements between variables to have space in between them since output field separator for awk is space by default but since I am NOT sure , 's behavior in gnuplot so adding OFS here which also is a way to pace space.我们实际上可以在变量之间的print语句中使用逗号来在它们之间留出空格,因为 output 字段分隔符awk默认是空格,但由于我不确定,所以在 gnuplot 中的行为,所以在这里添加OFS这也是调整空间的一种方式.

Is this essentially what you're trying to do?这基本上是你想要做的吗?

$ sh -c "awk 'BEGIN{yr=2016; y=1; a[y]=56; print (yr++) \" (\" a[y] \" Loads)\" }'"
2016 (56 Loads)

or this:或这个:

$ sh -c "awk 'BEGIN{yr=2016; y=1; a[y]=56; print \"\\\"\"(yr++) \" (\" a[y] \" Loads)\\\"\" }'"
"2016 (56 Loads)"

or from a call to system():或来自对 system() 的调用:

$ awk 'BEGIN{system("awk \"BEGIN{yr=2016; y=1; a[y]=56; print \\\"\\\\\\\"\\\"(yr++) \\\" (\\\" a[y] \\\" Loads)\\\\\\\"\\\" }\"")}'
"2016 (56 Loads)"

and with fewer backslashes:并且使用更少的反斜杠:

$ awk 'BEGIN{system("awk -v dq=\"\\\"\" \"BEGIN{yr=2016; y=1; a[y]=56; printf \\\"%s%d (%d Loads)%s%s\\\", dq, yr++, a[y], dq, ORS }\"")}'
"2016 (56 Loads)"

You tagged your question as gnuplot , so I assume you want to plot your data with gnuplot.您将问题标记为gnuplot ,所以我假设您想使用 gnuplot plot 您的数据。 So, why "messing" around with bash or awk if you can do it with gnuplot only?那么,如果您只能使用 gnuplot 来“搞砸” bash 或 awk ,为什么还要“搞砸”呢? Admittedly, it's not an obvious or conventional gnuplot script.诚然,它不是一个明显或传统的 gnuplot 脚本。 Maybe there are even better ways with gnuplot-only.也许只有 gnuplot 有更好的方法。

Script:脚本:

### sum up columns
reset session

$Data <<EOD
vol.dat
Jan    0   165   165   228    78
Feb   10    52   149   196    79
Mar   46   186   159   137   182
EOD

set style fill solid 0.5
set boxwidth 0.8
set key top left reverse noautotitle
set xtics out
set grid y
set offset 0,0,50,0

Totals = ''
do for [col=2:6] {
    stats $Data u col nooutput
    Totals = Totals.sprintf(" %g",STATS_sum)
}
Total(col) = real(word(Totals,int(column(col))+1))
N          = words(Totals)-1
StartYear  = 2016
set xrange [StartYear-0.5:] noextend

plot '+' u ($0+StartYear):(Total(0)):($0+1) every ::::N w boxes lc var, \
     '+' u ($0+StartYear):(a=Total(0)):(sprintf("%g",a)) every ::::N w labels offset 0,1
### end of script

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM