This time, even the duff sort key got it right. But, if the “3” in column 2 is changed to a “6”, making it the same for both lines, things change…īash-4.2$ cat 2 | sort -b -k 4.7,4.8 -k 4.1,4.2 -k 4.4,4.5 # Correct sort again Picking some new, simpler data shows the difference though:īash-4.2$ cat 2 | sort -b -k 4.7,4.8 -k 4.1,4.2 -k 4.4,4.5 # Correct sort, by Moīash-4.2$ cat 2 | sort -b -k 4.7,4.8 -k 4.1,4.2 -k 4.3,4.4 # Incorrect sortĪs expected, the incorrect sort key gets it wrong. Įven with the incorrect “-k 4.3,4.4”, it still appeared to sort dates in the right order, at least with the file above. This entry was posted in Linux, Unix and tagged -b, -k, column, date, field, linux sort, multiple, sort, sort -b, sort -k, time, unix sort by Jim. Useful for sorting by dates, times, serial numbers, custom fields, whatever. Just remember to always use “ -b” with “ -k“, and it works. None of which really explains what is happening on the command line. On Linux it adds “ If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace“. The Solaris man page says that -b “ ignores leading blank characters“. Those -k switches mean: sort by characters 7 to 8 of the 4th field (the year), then by characters 1 to 2 (the month) and finally by characters 4 to 5 (the day). Those three “ -k” arguments should sort by the year, then the month, then the day, respectively, putting the lines in overall date order. We want to sort by the date in the 4th column (American format – mm/dd/yy). Tricky until you realize it never works you also supply the “ -b” argument. But the man page isn’t the clearest, and getting the “ -k x.y” notation to work is tricky. Parts of fields can be further specified with “ -k n.m“, says the man page.įor example, “ sort -k 2.3” should sort by the second field, starting with the third character in that field. For example, “ sort -k 2” will sort by the second field on each line of input. The “ sort” command on Solaris has a “ -k” switch for sorting by a particular field.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |