Awk/Unix group by in order -
assume have text file:
daisy abc0002 1 kevin abc0001 2 mike abc0001 3 john abc0003 4 pete abc0002 5
we try such result:
kevin abc0001 2 mike abc0001 3 daisy abc0002 1 pete abc0002 5 john abc0003 4
there no order among names last column should considered while grouping rows according abc000# labels. suggestion? thanks.
for data shown, can use:
sort -k2,2 -k3,3
if there might ever multi-digit numbers in third column, specify numeric sorting:
sort -k2,2 -k3,3n sort -k2,2 -k3n,3
for example, given input file:
daisy abc0002 1 kevin abc0001 2 mike abc0001 3 john abc0003 4 pete abc0002 5 hazel abc0002 34 sarah abc0002 24 alice abc0002 11 zoe abc0002 9
numeric sort in column 3
kevin abc0001 2 mike abc0001 3 daisy abc0002 1 pete abc0002 5 zoe abc0002 9 alice abc0002 11 sarah abc0002 24 hazel abc0002 34 john abc0003 4
non-numeric sort in column 3
kevin abc0001 2 mike abc0001 3 daisy abc0002 1 alice abc0002 11 sarah abc0002 24 hazel abc0002 34 pete abc0002 5 zoe abc0002 9 john abc0003 4
note zoe , pete appear in 2 outputs.
Comments
Post a Comment