Custom sorting in shell -
i have custom order sorting: 'd','r' -> 'i' -> 'w','x' , 'z'. is, 'd' , 'r' equal , sort before 'i'; , 'w', 'x' , 'z' equal , sort after 'i'.
input:
123d123 234r111 333i333 111w111 222x222 111z111
the sorting on 4th character, , followed sorting 5th 7th characters.
the expected output be:
234r111 123d123 333i333 111w111 111z111 222x222
paste <(cut -c 4-7 file | tr 'rwx' 'dzz') <(cat file) | sort -k 1,1 | awk '{print $2}' 234r111 123d123 333i333 111w111 111z111 222x222
explanation: create key r=d , w=x=z, sort key, discard key
Comments
Post a Comment