0

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)

3
  • I cannot reproduce this problem. Could you provide us with a minimal example which creates this problem? Small version of vol.dat Commented Jun 4, 2020 at 14:42
  • edit your question to include a minimal reproducible example with concise, testable sample input and expected output. See How to Ask. You should make your example in terms of calling awk from sh -c "..." instead of from gnuplot's system("...") since relatively few people will have the latter to test with and the same concerns almost certainly apply to both. Commented Jun 4, 2020 at 14:51
  • Is Output the output you get or the output you want? Please make sure to show us the output you want. Commented Jun 4, 2020 at 19:30

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

nope I get the 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) ^ "./plotFancy.pg", line 45: ')' expected
@charlie0440, ohh while copying your attempt " got missed you could try now and lemme know please how it goes.
2

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

2 Comments

no, see updated question. I think part of the problem maybe that the output from awk needs to come back enclosed in double quotes for gnuplot accept each line as a single input eg "2016 (56 Loads)"
OK, I updated my answer to add quotes around the output. Is that it?
0

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

Comments

0

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

1 Comment

I don't see the relation of this answer to your original question. There is no gnuplot script (except the system/awk command) and no terminal, text or title mentioned in your question. Sorry to say, but if you don't post a minimal, reproducible gnuplot script, this question and this answer are good for nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.