In an attempt to solve a question, I wrote the following gnu-awk
script and ran into an issue with sort (should have read the manual first).
From the manual:
Because IGNORECASE affects string comparisons, the value of IGNORECASE also affects sorting for both asort() and asorti(). Note also that the locale's sorting order does not come into play; comparisons are based on character values only.
This was the proposed solution:
awk '{
lines[$0]=length($0)
}
END {
for(line in lines) { tmp[lines[line],line] = line }
n = asorti(tmp)
for(i=1; i<=n; i++) {
split(tmp[i], tmp2, SUBSEP);
ind[++j] = tmp2[2]
}
for(i=n; i>0; i--)
print ind[i],lines[ind[i]]
}' file
aaaaa foo 9
aaa foooo 9
aaaa foo 8
aaa foo 7
as foo 6
a foo 5
aaaaaaa foooo 13
I tried adding 0 to force numeric type, however wasn't able to reach the desired output. Is there a way we can simulate numeric sort in awk/gawk
?
Input File:
aaa foooo
aaaaaaa foooo
a foo
aaa foo
aaaaa foo
as foo
aaaa foo
Desired Output:
aaaaaaa foooo
aaaaa foo # Doesnt matter which one comes first (since both are same size)
aaa foooo # Doesnt matter which one comes first (since both are same size)
aaaa foo
aaa foo
as foo
a foo
The numbers shows in the script output is just for illustration on how sorting was done.