sorting - Unix sort emits lines outside of expected order only when second column exists -
i have file 2 columns. first column has 2 underscore-separated numbers in it, , want sort file lexicgraphically column. now, if there no second column, default sort precisely want:
$ { echo 211_284; ((i=2840;i<=2842;++i)); echo 211_$i; done; echo 211_284; } | sort -k1 211_284 211_284 211_2840 211_2841 211_2842
but if add second column (which should irrelevant sort!):
$ { echo 211_284 x; ((i=2840;i<=2842;++i)); echo 211_$i y; done; echo 211_284 z; } | sort -k1 211_2840 y 211_2841 y 211_2842 y 211_284 x 211_284 z
or adding second column 1 of rows:
$ { echo 211_284 x; ((i=2840;i<=2842;++i)); echo 211_$i; done; echo 211_284; } | sort -k1 211_284 211_2840 211_2841 211_2842 211_284 x
how sort on first column, real?
if want ignore other first column, use sort -k1,1
; otherwise, you're specifying start column not end column:
also, if don't want locale's collation order impact lexographic sort relationship between digits , spaces, set lc_all=c
explicitly (or, more narrowly, lc_collate=c
).
$ { echo 211_284 x; ((i=2840;i<=2842;++i)); echo 211_$i y; done; echo 211_284 z; } \ > | lc_all=c sort -k1,1 211_284 x 211_284 z 211_2840 y 211_2841 y 211_2842 y
Comments
Post a Comment