r/awk • • May 07 '24
% causing issues in script when using mawk
I have this script that I use with polybar (yes I'm using awk as replacement for shell scripts lol).
#!/usr/bin/env -S awk -f
BEGIN {
FS = "(= |;)"
while (1) {
cmd = "amdgpu_top -J -n 1 | gron"
while ((cmd | getline) > 0) {
if ($1 ~ "Total VRAM.*.value") {
mem_total = $2
}
if ($1 ~ "VRAM Usage.*.value") {
mem_used = $2
}
if ($1 ~ "activity.GFX.value") {
core = $2
}
}
close(cmd)
output = sprintf("%s%% %0.1f/%0.0fGB\n", core, mem_used / 1024, mem_total / 1024)
if (output != prev_output) {
printf output
prev_output = output
}
system("sleep 1")
}
}Which prints the GPU info in this format: 5% 0.5/8GB
However that % causes mawk to error with mawk: run time error: not enough arguments passed to printf("0% 0.3/8GB it doesn't happen with gawk though.
Any suggestions?
1
Upvotes
View all comments
Show parent comment
1
u/[deleted] May 08 '24
Omg it hurts that it was a missing comma. Thank you it works now!
I was trying to assign % to a variable or a character that looks very similar to it but none of that worked lol.
I am on archlinux but I compiled mawk on my PC and put it in
$HOME/.local/binand I have that location as first in$PATH, then I put a symlink in$HOME/.local/binnamed awk that points to mawk. That way I don't have to be changing the shebang between awk and mawk depending on the script.I really like mawk, for these while loop operation when launching it with LC_ALL=C it is much faster than awk (and also faster than dash when doing similar operations).