SUSE Support

Here When You Need Us

Results of sort command are out of order.

This document (7001026) is provided subject to the disclaimer at the end of this document.

Environment

SUSE Linux Enterprise Desktop (SLED) Server (SLES) or openSUSE.

Situation

Command line utility "sort" is being used to sort the lines whithin a text file.  A specific field is the sort key but the data in that field may identical for multiple entries.  The expected result is that the entries with the same sort key field would remain in the original order.  The actual result is that the entries are randomized.

Resolution

Linux provides many command utilities and there are almost always multiple ways to accomplish the same thing.  Here is one suggestion to keep the original line order for those entries with identicle sort key fields:

nl -ba < SourceTextFile.dat | sort -k 4,4 -k 1n | cut -f2- > SortedResults.txt

Here's a breakdown of this command:

nl -ba < SourceTextFile.dat
In this example the original source data file is "SourceTextFile.dat".  "nl -ba" is run and the source data file is redirected as input for that command.  The result is the same information with a line number at the beginning of each line.  This line numbering will preserve the order for us in the sort command.

| sort -k 4,4 -k 1n
Next we use the pipe symbol "|" to send the new results to the sort command which says to sort the lines in the file based on the first key field "4,4" then as a secondary sort use the key field 1 and treat it as numerical (n).  Field 4 is just an example and could be changed to any field you need to sort on.  Field one is actually the field number you will need to use for sorting on the line numbers that have been added to each line.

Remember, since the line numbers have been added your data fields will now be incremented one to the right.

As a side note, "-k 4,4" is used to identify the beginning and ending point of the field to use for the primary sort. If the ending field was not specified then sort would assume the second "-k 1n" to be the ending value and attempt to sort on everything between 4 and 1.

| cut -f2- > SortedResults.txt
Finally, the line numbers will need to be removed from the entries and the results written to a file.  The pipe symmbol "|" is used to send the now sorted information to the "cut" command.  The "-f2-" says to display field 2 and all fields following to the end of the line, effectively removing the line numbers which are in field 1.  That final output is redirected to the file "SortedResults.txt" which should be changed to whatever file name you would like to use.

Additional Information

Feedback was received on this article suggesting that "-s" or "--stable" (stabilize sort by disabling last-resort comparison) could also be used.  For the customer's data used to test the solution above the "-s" had no effect on the outcome, but it would be easy to test this option.

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.

  • Document ID:7001026
  • Creation Date: 24-Jul-2008
  • Modified Date:03-Mar-2020
    • SUSE Linux Enterprise Desktop
    • SUSE Linux Enterprise Server

< Back to Support Search

For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com

tick icon

SUSE Support Forums

Get your questions answered by experienced Sys Ops or interact with other SUSE community experts.

tick icon

Support Resources

Learn how to get the most from the technical support you receive with your SUSE Subscription, Premium Support, Academic Program, or Partner Program.

tick icon

Open an Incident

Open an incident with SUSE Technical Support, manage your subscriptions, download patches, or manage user access.