To sort by multiple fields using the sort command, you can specify multiple -k options, each followed by the field number you want to sort by. The sort will prioritize the first field specified, then the second, and so on.
Here's the general syntax:
sort -k<N1> -k<N2> -k<N3> ...
Example
Suppose you have a file data.txt with the following content:
Alice,30,5000
Bob,25,6000
Alice,25,7000
Bob,30,4000
To sort this file first by the second field (age) and then by the third field (salary), you would use:
sort -t, -k2,2n -k3,3n data.txt
-t,: Sets the delimiter to a comma.-k2,2n: Sorts by the second field (age) numerically.-k3,3n: Sorts by the third field (salary) numerically.
This command will first sort the entries by age and, for entries with the same age, it will sort them by salary.
If you have any more questions or need further clarification, feel free to ask!
