The syntax for the sort command in Unix/Linux is as follows:
sort [OPTION]... [FILE]...
Common Options
Here are some commonly used options with the sort command:
-k FIELD: Sort by the specified field (column). You can specify multiple-koptions for sorting by multiple fields.-t DELIMITER: Specify a custom delimiter for separating fields (default is whitespace).-n: Sort numerically (useful for sorting numbers).-r: Reverse the sort order (sort in descending order).-u: Suppress duplicate lines (only unique lines will be output).-o FILE: Write the output to the specified file instead of standard output.-f: Ignore case when sorting.
Example Syntax
-
Basic Sort:
sort input_file.txt -
Sort with a Custom Delimiter:
sort -t ',' -k 2 input_file.txt -
Sort Numerically:
sort -n -k 2 input_file.txt -
Sort in Descending Order:
sort -r -k 2 input_file.txt -
Sort by Multiple Fields:
sort -t ',' -k 2,2 -k 3,3 input_file.txt -
Sort and Write to a File:
sort -o sorted_output.txt input_file.txt
Summary
The sort command is versatile and can be customized using various options to meet specific sorting needs. You can combine options as needed to achieve the desired sorting behavior.
