If you want to show all lines having the 2nd field in the [0,999] range and finally show how many of these columns are, then this can make it. No need to sort -n and wc -l, awk itself can do it:
$ awk -F, '($2 >= 0 && $2 <= 999) {printf "%10s %40s\n" , "Total", $2; i++} END{print "total: "i}' file
Total 1
Total 2
Total 4
Total 3
total: 4
And if you just want the sum:
$ awk -F, '($2 >= 0 && $2 <= 999) i++} END{print "total: "i}' file
total: 4
Why was your command not working?
With print:
$ awk -F, '{if($2 >= 0 && $2 <= 999) print "%10s" "%40s" , "Total", $2}' a | sort -n
%10s%40s Total 1
%10s%40s Total 2
%10s%40s Total 3
%10s%40s Total 4
The number of lines was ok, although the format was not: to use %10s you need to use printf instead.
With printf:
$ awk -F, '{if($2 >= 0 && $2 <= 999) printf "%10s" "%40s" , "Total", $2}' a
Total 1 Total 2 Total 4 Total 3
As per using printf without printing new lines, all output was in the same line. You just needed to add \n at the end of each printf:
$ awk -F, '{if($2 >= 0 && $2 <= 999) printf "%10s %40s\n", "Total", $2}' file
Total 1
Total 2
Total 4
Total 3
printfinstead ofprint.