Pls i need help rounding up multiple columns in this .csv output. So far, i've been able to pipe my jq output into awk and round up just a single column but don't know how to round up this 6 columns.
This is the demo.json payload
{
"XXXX": "XXX",
"XXXXX": "XXXXXXX",
"stack": {
"123": {
"overflow": 6014.4703
},
"234": {
"overflow": 6090.3888
},
"356": {
"overflow": 8973.8136
},
"244": {
"overflow": 5034.4258
},
"789": {
"overflow": 5957.6018
},
"545": {
"overflow": 1225.7363
}
}
}
Below is the line of code i have so far for a single column roundup
cat demo.json | jq -r --argjson H "$(date %H -d "1 hour ago")" '["Hours", "A", "B", "C", "D", "E", "F"], ([.stack."123".overflow/1000, .stack."234".overflow/1000, .stack."356".overflow/1000, .stack."244".overflow/1000, .stack."789".overflow/1000, .stack."545".overflow/1000] | [$H] .) | @csv' | awk 'BEGIN{ FS=OFS="," }NR>1{ $3=sprintf("%.2f", $3) }1' > demo.csv
What i'm trying to achieve
"Hour","A","B","C","D","E","F"
"09","6.01","6.09","8.97","5.03","5.96","1.23"
CodePudding user response:
Update |=
the relevant columns / fields as follows: Divide by 10
, then round
, and divide again by 100
.
.stack[].overflow |= (. / 10 | round / 100)
{
"XXXX": "XXX",
"XXXXX": "XXXXXXX",
"stack": {
"123": {
"overflow": 6.01
},
"234": {
"overflow": 6.09
},
"356": {
"overflow": 8.97
},
"244": {
"overflow": 5.03
},
"789": {
"overflow": 5.96
},
"545": {
"overflow": 1.23
}
}
}
CodePudding user response:
Add more sprintf
s in awk
separated by semi colons:
... | awk 'BEGIN{ FS=OFS="," }NR>1{ $2=sprintf("%.2f", $2); $3=sprintf("%.2f", $3); $4=sprintf("%.2f", $4); ...etc... }1' > demo.csv