tag:blogger.com,1999:blog-11265228.post8122097066920833098..comments2024-03-28T02:37:24.688-07:00Comments on tech: Linux sort : bug with , separator and confusing period?Unknownnoreply@blogger.comBlogger3125tag:blogger.com,1999:blog-11265228.post-1514789040063224112012-01-20T20:56:12.013-08:002012-01-20T20:56:12.013-08:00Although, that is due to the shell not sending a p...Although, that is due to the shell not sending a proper tab character to the sort program.<br /><br />This works:<br /><br />[~] echo -e "alan\t20\t3\t0\ngeorge\t5\t0\t0\nalice\t3\t5\t0\ndora\t4\t9\t5" | LC_ALL=C sort -n -k 2 -t"`echo -e "\t"`"<br />alice 3 5 0<br />dora 4 9 5<br />george 5 0 0<br />alan 20 3 0thusharahttps://www.blogger.com/profile/09820727533887579134noreply@blogger.comtag:blogger.com,1999:blog-11265228.post-56953600038060850732011-03-17T16:28:47.833-07:002011-03-17T16:28:47.833-07:00Actually I found a simpler case where LC_ALL has n...Actually I found a simpler case where LC_ALL has no effect:<br /><br />echo -e "alan\t20\t3\t0\ngeorge\t5\t0\t0\nalice\t3\t5\t0\ndora\t4\t9\t5" | LC_ALL=C sort -n -k 2 -t \tthusharahttps://www.blogger.com/profile/09820727533887579134noreply@blogger.comtag:blogger.com,1999:blog-11265228.post-71019796777532686212011-03-17T11:06:26.995-07:002011-03-17T11:06:26.995-07:00This will solve the sort problem
sort -t , -k 2n,...This will solve the sort problem<br /><br />sort -t , -k 2n,2 file-to-be-sorted.txt<br /><br />You need “-k 2,2″ to tell sort to only consider the second column. The “n” in “-k 2n,2″ tells the sort command to sort the column numerically. If you omit the “n” 20 gets sorted before 3.<br /><br />I did some testing and it seems that when you sort multiple numerical columns the numbers gets combined. When you specify “-k 2″ you tell sort to use all columns from column two until the end of the line. You also specified comma to be the column separator. When you do this then I think sort see the data this way:<br /><br />the original data<br /><br />dora,4,0.9,5<br />alice,3,5,0<br />george,5,0,0<br />alan,20,3,0<br /><br />becomes<br /><br />dora,40.95<br />alice,350<br />george,500<br />alan,2030<br /><br />Which is in the correct order. I did a test with different numbers and still got the same behavior.<br /><br />It's interesting that LC_PATH=C affects the sort.Michaelhttp://www.btaz.com/unixlinux/unixlinux-sort-multiple-columns-tab-delimited-and-reverse-sort-order/noreply@blogger.com