MS-DOS redirection with SORT: To sort a file's contents in ascending order and write the sorted result to the screen, which form correctly feeds the file to the SORT command?

Difficulty: Medium

Correct Answer: SORT < filename

Explanation:


Introduction / Context:
DOS commands often use redirection to supply input or capture output. The SORT utility reads lines from standard input and writes sorted lines to standard output by default (ascending order). Choosing the correct redirection operator is essential for the desired behavior.


Given Data / Assumptions:

  • You have a text file to be sorted in ascending order.
  • You want SORT to read from the file without changing the file name or creating a new file.
  • Default SORT direction is ascending unless otherwise specified by switches.


Concept / Approach:
Input redirection in DOS uses the less-than symbol. “SORT < filename” sends the file's content to SORT via standard input. “SORT filename” treats “filename” as a literal string input on some DOS variants or may be rejected; the canonical method is input redirection. “SORT > filename” writes output to a file but does not feed input. Pipes “|” connect outputs to inputs of programs, not files directly.


Step-by-Step Solution:

Identify the tool behavior: SORT reads from stdin, writes to stdout.Apply input redirection: use “<” to feed the file's contents to stdin.Choose “SORT < filename” as correct.


Verification / Alternative check:
Run “TYPE filename | SORT” to confirm equivalent behavior. Compare with “SORT > out.txt” which captures output but still requires input via stdin.


Why Other Options Are Wrong:
SORT filename: not the canonical usage for DOS SORT input. SORT > filename: redirects output only. SORT | filename: invalid piping to a file; pipes connect program to program.


Common Pitfalls:
Confusing input and output redirection; forgetting that SORT consumes stdin by default; overwriting files unintentionally with output redirection.


Final Answer:
SORT < filename

More Questions from Disk Operating System (DOS)

Discussion & Comments

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