Difficulty: Easy
Correct Answer: sort
Explanation:
Introduction / Context:
Sorting text is a foundational UNIX task used in data processing pipelines. The standard utility for ordering lines is designed to read from files or standard input and produce sorted output to standard output, integrating seamlessly with other tools via pipes.
Given Data / Assumptions:
Concept / Approach:
The command sort
performs lexicographic sorting by default in ascending order. Options can modify behavior: -r
reverses the order, -n
performs numeric compares, and locale variables can change collation rules.
Step-by-Step Solution:
Use: sort input.txt > output.txt to sort lines ascending.Combine with pipes: cat input.txt | sort | uniq for de-duplicated sorted output.For reverse order, add -r; for numeric fields, add -n or -k as needed.
Verification / Alternative check:
Compare outputs from sort
and sort -r
to see ascending vs. descending order. Use LC_ALL=C
for byte-wise ordering when locale collation confuses results.
Why Other Options Are Wrong:
sh (Option B) is the shell; it does not sort by itself.st (Option C) is not a standard command.sort -r (Option D) explicitly requests reverse (descending) order, not ascending alphabetical by default.
Common Pitfalls:
Final Answer:
sort
Discussion & Comments