Practical Sorting Examples
Real-World Numeric Sorting Scenarios
## Sort processes by memory usage
ps aux | sort -rnk4 | head -n 10
Log File Processing
## Extract and sort error codes
grep "ERROR" system.log | awk '{print $5}' | sort -n | uniq -c
Data Manipulation Techniques
Sorting Numerical Columns
graph TD
A[Sorting Strategies] --> B[Column-Based Sorting]
A --> C[Unique Value Extraction]
A --> D[Numeric Comparison]
Complex Sorting Example
## Sort CSV file by third numeric column
cat data.csv | sort -t',' -k3 -n
Network and System Monitoring
IP Address Sorting
## Sort IP addresses numerically
cat ip_list.txt | sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4
Sorting Scenario |
Recommended Command |
Performance Consideration |
Large Files |
sort -n |
Use memory-efficient options |
Unique Values |
sort -nu |
Reduce redundant data |
Reverse Order |
sort -nr |
Quick descending sort |
Advanced Sorting Techniques
Combining Multiple Criteria
## Sort by multiple columns
cat system_stats.txt | sort -t':' -k2,2n -k3,3n
Practical Tips for LabEx Users
- Always use
-n
for numeric sorting
- Understand column separation
- Leverage pipe (
|
) for complex sorting
- Test sorting scripts in controlled environments
Error Handling and Validation
## Validate numeric sorting
[ $(echo -e "1\n2\n3" | sort -n | tail -n 1) -eq 3 ] && echo "Sorting Successful"