The issue lies in how you're concatenating strings in the awk script. In awk, the += operator does not work the same way as in other programming languages like Bash or Python. Specifically, += in awk is used for arithmetic operations, not string concatenation. When you use += with strings, awk tries to interpret the string as a number, and if it fails, it treats it as 0. This is why your script is printing 0 instead of the expected string.
To fix this, you should use proper string concatenation in awk. In awk, strings are concatenated simply by placing them next to each other without any operator. Here's the corrected version of your script:
echo "${statistics_result_string}" | awk --assign environment=${environment} --assign measurement_name=${measurement_name} '{
output_influxdb_string="resource_usage_" environment
output_influxdb_string=output_influxdb_string ",name=" measurement_name
output_influxdb_string=output_influxdb_string " " $1 "_count=" $2
output_influxdb_string=output_influxdb_string ", " $1 "_min=" $3
output_influxdb_string=output_influxdb_string ", " $1 "_10p=" $4
output_influxdb_string=output_influxdb_string ", " $1 "_25p=" $5
output_influxdb_string=output_influxdb_string ", " $1 "_50p=" $6
output_influxdb_string=output_influxdb_string ", " $1 "_75p=" $7
output_influxdb_string=output_influxdb_string ", " $1 "_90p=" $8
output_influxdb_string=output_influxdb_string ", " $1 "_max=" $9
print output_influxdb_string
}'
Key Changes:
- Replaced
+= with = and explicitly concatenated strings using output_influxdb_string=output_influxdb_string "...".
- In
awk, string concatenation is implicit when you place strings next to each other, so "a" "b" results in "ab". However, for clarity, I used output_influxdb_string=output_influxdb_string "...".
This should resolve the issue, and your script will now produce the expected output.