Overview of sort
`sort` command compares each line as a whole and sorts it in ASCII character order (dictionary order) before sending the results to standard output. The original file is not modified, and to save the changes to a file, redirection (`>`) must be used.
Main Functions of sort
`sort` command is primarily used for the following purposes:
Main Use Cases
- Data Sorting: Sorts log files, data lists, user lists, etc., based on specific criteria to improve readability.
- Duplicate Removal: Easily removes duplicate lines based on sorted data. (Used with `uniq`)
- Report Generation: Sorts data based on specific fields to create a more analyzable format.
- File Comparison: Can be used to normalize contents of two files before comparison.
Basic Principles of Sorting
`sort` primarily starts comparing from the first character of each line based on character code values (ASCII values). Since numbers are treated as strings, unexpected results may occur, such as '10' coming before '2'. To avoid this, numeric sorting options should be used.
Main Options for the sort Command
`sort` command provides various options to finely control sorting criteria, order, and duplicate handling.
1. Sorting Criteria and Order
2. Duplicate and Whitespace Handling Options
3. Output and Delimiter Options
Generated command:
Try combining the commands.
Description:
`sort` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to efficiently sort and analyze text data through various examples of the `sort` command.
Basic Sorting of File Contents
echo -e "Charlie\nAlice\nBob" > names.txt
sort names.txt
Sorts the lines of the `names.txt` file in alphabetical order (ascending) and outputs them.
Reverse (Descending) Sorting of File Contents
echo -e "10\n2\n100" > numbers.txt
sort -r numbers.txt
Sorts the numbers in the `numbers.txt` file in reverse order, treating them as strings.
Sorting Based on Numeric Values
echo -e "20 Bob\n10 Alice\n100 Charlie" > scores.txt
sort -n scores.txt
Sorts the scores (first field) in the `scores.txt` file in ascending order based on actual numeric values.
Sorting Based on the Second Column (Name) of a CSV File
echo -e "1,Bob,Seoul\n3,Alice,Busan\n2,Charlie,Jeju" > users.csv
sort -t',' -k 2 users.csv
Sorts the second field (name) of the `users.csv` file, which is comma (`,`) separated.
Removing Duplicate Lines After Sorting
echo -e "apple\nbanana\napple\ncherry" > duplicates.txt
sort -u duplicates.txt
Sorts the contents of the `duplicates.txt` file and removes duplicate lines, keeping only one.
Saving Sorted Results to a New File
echo -e "Zebra\nCat\nDog" > unsorted.txt
sort unsorted.txt -o sorted.txt
Sorts the contents of the `unsorted.txt` file and saves the results to the `sorted.txt` file. The original file remains unchanged.
Sorting by Human-Readable Size
echo -e "10K file1\n2M file2\n500K file3\n1G file4" > sizes.txt
sort -h sizes.txt
Sorts a list of file sizes considering human-readable units (K, M, G). Useful for sorting results from commands like `du -h`.