-2

The code snippet below extracts values from an xml file and store totals for days 1 to 7 and a grand total for the last 7 days - $lsd. The day figures are 6 digit numbers with 3 decimal places. I cannot make the 'awk' function work. Everything I have tried produces either "0.000" or "". Changing the INC operator to simple addition gives similar results

Needless to say, I'm a complete newbie to Bash Arithmetic. Please can somebody tell me where I'm going wrong.

    for i in {1..7} ;
    do
        day=$(grep -oPm1 "(?<=<d00$i>)[^<]+" < data.xml) 
        printf "%.3f" $day > day$i.txt ;
        awk 'BEGIN{lsd += $day}' 
    done ;
    printf "%.3f" $lsd > lsd1.txt 
Barmar
  • 741,623
  • 53
  • 500
  • 612
Sabreur
  • 21
  • 7
  • Can you show us the format of data.xml? You can do the arithmetic with bash and the bc command. E.g. this command `bc <<< $(echo "scale=3; 1.442 + 3.965")` prints 5.407 – John Jan 20 '23 at 17:32
  • 2
    Use proper tools to parse xml not just grep – Diego Torres Milano Jan 20 '23 at 17:39
  • 2
    Please [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Jan 20 '23 at 17:45
  • 2
    Please add data.xml and your desired output to your question (no comment here). – Cyrus Jan 20 '23 at 17:46
  • Also, probably has nothing to do with bash arithmetic if it's (correctly) using `awk` – Diego Torres Milano Jan 20 '23 at 17:48
  • Variables aren't expanded inside single quotes. So you're not using the bash variable `$day` in the `awk` command. See https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script – Barmar Jan 20 '23 at 18:08
  • And `awk` variables don't become shell variables. You never set `$lsd`. – Barmar Jan 20 '23 at 18:09
  • `awk` needs an input source. If you don't specify a filename, it reads from standard input. So it's probably waiting for you to type something. – Barmar Jan 20 '23 at 18:11
  • @Barmar : it wouldn't hang - it already has a `BEGIN { }` section, so it'll just process those then instantly exit, without printing anything to anywhere :: `gawk -p- 'BEGIN { lsd += $day }' ::::::::: # gawk profile, created Fri Jan 20 13:41:09 2023 ::::::::: # BEGIN rule(s) ::::::::: BEGIN { :::::::::: 1 lsd += $day }` – RARE Kpop Manifesto Jan 20 '23 at 18:39
  • As the bash tag you used instructs - "For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting them here." – Ed Morton Jan 20 '23 at 22:35

1 Answers1

2

Please can somebody tell me where I'm going wrong.

(...)
awk 'BEGIN{lsd += $day}'
(...)
printf "%.3f" $lsd > lsd1.txt

does suggests that you are promptly ignoring that awk's variables and bash's variables are separate from each other, consider following example

x=10
awk 'BEGIN{x+=1;print x}' emptyfile
echo $x

does output

1
10

Observe that inside awk command x is unset, thus when increment by 1 is request it is assumed to be 0 thus print x does give 1 and awk did not anything to bash's x as shown by line with echo.

(tested in GNU Awk 5.0.1)

Daweo
  • 31,313
  • 3
  • 12
  • 25