I am using awk and printing in this format print $5,$3,$6,$7,$8,$9, so that sort would use the values in $5 and print in descending order. But I get random sorting
I am not in front of a solaris host at the moment but IIRC later versions of solaris du -h will display file sizes in human readable format - that will be easier to sort than ls -lh.
Index is a built-in function in awk that finds the position of a string within another.
For example index("the quick brown fox", "r") would return 12 as the first "r" found in the string is 12 characters from the front on the string.
the string KMGTPEZY represents the suffixes (Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta). So index gives us 1 for K, 2 for M, 3 for G, etc. We can then raise 1024 to this number to get the conversion factor for the suffix.
is to undo what the ls -h option did and give you an approximation to the actual file size in bytes that can be sorted numerically. If you were willing to change the 1st command in your pipeline from ls -lh to just ls -l , those two statements could be removed from the awk script and the sed command (which deletes the file size in bytes from the output) could be removed from the end of the pipeline.