UNIX Text Sorting Basics Which command sorts the lines of a text file into ascending (alphabetical) order by default?

Computer Science Unix Difficulty: Easy
Choose an option
  • A
    sort
  • B
    sh
  • C
    st
  • D
    sort -r
  • E
    None of the above

Answer

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:

  • POSIX-like environment with the standard coreutils or equivalent.
  • Default locale provides sensible collation unless otherwise specified.
  • Goal is ascending alphabetical order without extra modifiers.

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:

  • Mistaking locale-sensitive sorting for errors; set LC_ALL appropriately.
  • Forgetting to redirect output and accidentally printing to screen.

Final Answer:sort

Discussion & Comments
No comments yet. Be the first to comment!
Join Discussion