Trying to reformat bytes in column to mb and gb based on if else.
awk -F ',' '{if ($NF >= "1073741824") {$NF=$NF/1024/1024/1024; gsub(/.*/, "&GB", $NF)} else { $NF=$NF/1024/1024; gsub(/.*/, "&MB", $NF)}}1'
But else statement is not working. If bytes are greater than or equal to 1gb convert it to gb else to mb.
>Solution :
One general issue is with the conditional; the current code is performing a string comparison when it should be performing a numeric comparison.
Consider:
# string comparison ($1 >= "300")
$ echo 100000 | awk '{if ($1 >= "300") print "greater than 300"}'
- returns nothing
# numeric comparison ($1 >= 300)
$ echo 100000 | awk '{if ($1 >= 300) print "greater than 300"}'
greater than 300
Rolling this into OP’s code (remove double quotes from around the 1073741824:
# old:
awk -F ',' '{if ($NF >= "1073741824") {$NF=$NF/1024/1024/1024; gsub(/.*/, "&GB", $NF)} else { $NF=$NF/1024/1024; gsub(/.*/, "&MB", $NF)}}1'
^^^^^^^^^^^^
# new:
awk -F ',' '{if ($NF >= 1073741824 ) {$NF=$NF/1024/1024/1024; gsub(/.*/, "&GB", $NF)} else { $NF=$NF/1024/1024; gsub(/.*/, "&MB", $NF)}}1'
^^^^^^^^^^
If this edit of the current code does not address the issue then please update the question with the updated code, sample input, (wrong) output generated by the code and the (correct) expected output.