Sort lines by number of words per line
You could do something like:
awk '{print NF,$0}' file | sort -nr | cut -d' ' -f 2-
We use awk
to prefix the number of fields to each line. We then sort
by that number and remove it with cut
.
In recent GNU awk
one can use PROCINFO
array to define many internal parameters including order in which array elements are printed (controlled by element "sorted_in"
). Thus we can built and array indexed with the value of NF" "NR
, which elements have value of $0
and print it in desired output, in your case that would be "@ind_num_desc"
:
awk '{a[NF" "NR]=$0}END{PROCINFO["sorted_in"]="@ind_num_desc"; for(i in a) print a[i]}' file
Perl one-liner:
print sort { split(' ',$a) <=> split(' ',$b) } <>;
If you want to break ties using alphabetical order:
print sort { split(' ',$a) <=> split(' ',$b) or $a cmp $b } <>;