charlie0440
charlie0440

Reputation: 35

gnuplot awk/bash escaping quoting

UPDATED:

I have this awk command I want to nest into a system("") command. 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")

Bash does not like it due to the nesting and I am obviously not escaping the quotes correctly. 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:

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)

Upvotes: 0

Views: 282

Answers (4)

charlie0440
charlie0440

Reputation: 35

Solved it. See Gnuplot, how to include a space character in key titles? The problem was that I needed to use Enhanced postscript: &{x} to make a space in the title

Upvotes: 0

theozh
theozh

Reputation: 26113

You tagged your question as gnuplot, so I assume you want to plot your data with gnuplot. So, why "messing" around with bash or awk if you can do it with gnuplot only? Admittedly, it's not an obvious or conventional gnuplot script. Maybe there are even better ways with gnuplot-only.

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:

enter image description here

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 204381

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():

$ 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)"

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133710

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.

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.

Upvotes: 2

Related Questions