'SOLVED' awk buggy - % = modulo

liszca

Active Member
May 8, 2020
67
1
28
22
Awk does not compare numbers correct.

Input data:
Code:
Filesystem                   Type     1048576-blocks   Used Available Capacity Mounted on
udev                         devtmpfs           7933      0      7933       0% /dev
tmpfs                        tmpfs              1598     10      1589       1% /run
rpool/ROOT/pve-1             zfs              322247   2910    319337       1% /
tmpfs                        tmpfs              7989     60      7929       1% /dev/shm
tmpfs                        tmpfs                 5      0         5       0% /run/lock
tmpfs                        tmpfs              7989      0      7989       0% /sys/fs/cgroup
rpool                        zfs              319337      1    319337       1% /rpool
rpool/data                   zfs              319337      1    319337       1% /rpool/data
rpool/ROOT                   zfs              319337      1    319337       1% /rpool/ROOT
rpool/data/subvol-110-disk-1 zfs                2048    235      1814      12% /rpool/data/subvol-110-disk-1
rpool/data/subvol-101-disk-3 zfs                1024      3      1022       1% /rpool/data/subvol-101-disk-3
rpool/data/subvol-101-disk-2 zfs               36864  26065     10800      71% /rpool/data/subvol-101-disk-2
rpool/data/subvol-100-disk-0 zfs                8192   1322      6871      17% /rpool/data/subvol-100-disk-0
rpool/data/subvol-104-disk-0 zfs                8192    916      7276      12% /rpool/data/subvol-104-disk-0
rpool/data/subvol-111-disk-0 zfs                8192   1025      7168      13% /rpool/data/subvol-111-disk-0
rpool/data/subvol-128-disk-0 zfs                8192    599      7593       8% /rpool/data/subvol-128-disk-0
rpool/data/subvol-114-disk-0 zfs              322444   3108    319337       1% /rpool/data/subvol-114-disk-0
rpool/data/subvol-110-disk-3 zfs               36864  19935     16930      55% /rpool/data/subvol-110-disk-3
rpool/data/subvol-110-disk-0 zfs                8192   3648      4545      45% /rpool/data/subvol-110-disk-0
rpool/data/subvol-106-disk-0 zfs                8192    599      7594       8% /rpool/data/subvol-106-disk-0
rpool/data/subvol-108-disk-0 zfs                8192    642      7551       8% /rpool/data/subvol-108-disk-0
rpool/data/subvol-117-disk-0 zfs                8192    862      7331      11% /rpool/data/subvol-117-disk-0
rpool/data/subvol-113-disk-0 zfs                8192    590      7603       8% /rpool/data/subvol-113-disk-0
rpool/data/subvol-119-disk-0 zfs                8192    624      7569       8% /rpool/data/subvol-119-disk-0
rpool/data/subvol-101-disk-0 zfs                8192     67      8126       1% /rpool/data/subvol-101-disk-0
rpool/data/subvol-105-disk-0 zfs                8192   1101      7092      14% /rpool/data/subvol-105-disk-0
bpool                        zfs              460015 161532    298483      36% /bpool
rpool/data/subvol-110-disk-4 zfs                4096    414      3683      11% /rpool/data/subvol-110-disk-4
rpool/data/subvol-114-disk-1 zfs               32768     50     32719       1% /rpool/data/subvol-114-disk-1
rpool/data/subvol-101-disk-1 zfs               83968  66216     17752      79% /rpool/data/subvol-101-disk-1
rpool/data/subvol-116-disk-0 zfs                8192    831      7362      11% /rpool/data/subvol-116-disk-0
rpool/data/subvol-102-disk-1 zfs                8192    620      7573       8% /rpool/data/subvol-102-disk-1
/dev/fuse                    fuse                 30      1        30       1% /etc/pve
nfs.mufi:/var/nfs/proxmox    nfs4              36864  26064     10800      71% /mnt/pve/remote-nfs.mufi-nfs
tmpfs                        tmpfs              1598      0      1598       0% /run/user/0

Bash:
df -TmP|awk -v x=70 '/\/$|subvol/ {if ( $6 >= x) print $6}'

Output (Expected):
Code:
71%
79%

Actual Output:
Code:
71%
8%
8%
8%
8%
8%
79%
8%
 
Last edited:
tried with gawk also, same result for me, my workaround is to devide the numbers so they are have only one position to the left side of the comma.

awk version:
Code:
GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
 
Hmm, still don't know why this does not work for you:

Code:
root@proxmox ~ > df -TmP | awk -v x=40 '/\/$|subvol/ {if ( $6 >= x) print $6}'
57%
root@proxmox ~ > awk --version
GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
 
While this is a bit off-topic (awk is not particularly related to PVE) - on a hunch I guess that the comparison is done on strings with the % added - does
Code:
df -TmP | awk -v x=58 '{if ( gensub(/%/,"","g",$6)+0 >= x) print $6}'
work for you?
 
While this is a bit off-topic (awk is not particularly related to PVE) - on a hunch I guess that the comparison is done on strings with the % added - does
Code:
df -TmP | awk -v x=58 '{if ( gensub(/%/,"","g",$6)+0 >= x) print $6}'
work for you?

That did it, don't know why I didn't think about the percent as modulo.
 
  • Like
Reactions: Stoiko Ivanov
glad that helped - please mark the thread as 'SOLVED' thanks!
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!